diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt index 2a98dfb8c84..5490b80026e 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaSymbolProvider.kt @@ -11,11 +11,8 @@ import com.intellij.psi.search.GlobalSearchScope import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.declarations.FirRegularClass -import org.jetbrains.kotlin.fir.declarations.FirTypeParameter -import org.jetbrains.kotlin.fir.declarations.addDefaultBoundIfNecessary +import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.impl.FirTypeParameterImpl -import org.jetbrains.kotlin.fir.declarations.visibility import org.jetbrains.kotlin.fir.generateValueOfFunction import org.jetbrains.kotlin.fir.generateValuesFunction import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass @@ -103,6 +100,7 @@ class JavaSymbolProvider( useLazyNestedClassifierScope = regularClass is FirJavaClass, existingNames = (regularClass as? FirJavaClass)?.existingNestedClassifierNames ) + val wrappedDeclaredScope = wrapScopeWithJvmMapped(regularClass.classId, declaredScope, useSiteSession, scopeSession) val superTypeEnhancementScopes = lookupSuperTypes(regularClass, lookupInterfaces = true, deep = false, useSiteSession = useSiteSession) .mapNotNull { useSiteSuperType -> @@ -119,7 +117,7 @@ class JavaSymbolProvider( } JavaClassUseSiteMemberScope( regularClass, useSiteSession, - JavaSuperTypeScope(regularClass, useSiteSession, superTypeEnhancementScopes), declaredScope + JavaSuperTypeScope(regularClass, useSiteSession, superTypeEnhancementScopes), wrappedDeclaredScope ) } } diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt index ad9ba6ee51a..179d96c91c5 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/JavaUtils.kt @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.fir.FirSession -import org.jetbrains.kotlin.fir.declarations.FirFunction import org.jetbrains.kotlin.fir.declarations.FirTypeParameter import org.jetbrains.kotlin.fir.declarations.FirValueParameter import org.jetbrains.kotlin.fir.diagnostics.DiagnosticKind @@ -19,10 +18,8 @@ import org.jetbrains.kotlin.fir.expressions.FirArrayOfCall import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.impl.* import org.jetbrains.kotlin.fir.impl.FirAbstractAnnotatedElement -import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod import org.jetbrains.kotlin.fir.java.declarations.FirJavaValueParameter import org.jetbrains.kotlin.fir.java.enhancement.readOnlyToMutable -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef import org.jetbrains.kotlin.fir.references.impl.FirErrorNamedReferenceImpl import org.jetbrains.kotlin.fir.references.impl.FirResolvedNamedReferenceImpl import org.jetbrains.kotlin.fir.resolve.constructClassType @@ -36,6 +33,7 @@ import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl import org.jetbrains.kotlin.fir.types.impl.FirResolvedTypeRefImpl +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.ir.expressions.IrConstKind import org.jetbrains.kotlin.load.java.structure.* import org.jetbrains.kotlin.load.java.structure.impl.JavaElementImpl @@ -319,91 +317,3 @@ private fun JavaType.toFirResolvedTypeRef( ) } -internal fun FirFunction<*>.computeJvmDescriptor(): String = buildString { - if (this@computeJvmDescriptor is FirJavaMethod) { - append(name.asString()) - } else { - append("") - } - - append("(") - for (parameter in valueParameters) { - appendErasedType(parameter.returnTypeRef) - } - append(")") - - if (this@computeJvmDescriptor !is FirJavaMethod || (returnTypeRef as FirJavaTypeRef).isVoid()) { - append("V") - } else { - appendErasedType(returnTypeRef) - } -} - -// TODO: primitive types, arrays, etc. -private fun StringBuilder.appendErasedType(typeRef: FirTypeRef) { - fun appendClass(klass: JavaClass) { - klass.fqName?.let { - append("L") - append(it.asString().replace(".", "/")) - } - } - - when (typeRef) { - is FirResolvedTypeRef -> appendConeType(typeRef.type) - is FirJavaTypeRef -> { - when (val javaType = typeRef.type) { - is JavaClassifierType -> { - when (val classifier = javaType.classifier) { - is JavaClass -> appendClass(classifier) - is JavaTypeParameter -> { - val representative = classifier.upperBounds.firstOrNull { it.classifier is JavaClass } - if (representative == null) { - append("Ljava/lang/Object") - } else { - appendClass(representative.classifier as JavaClass) - } - } - else -> return - } - append(";") - } - } - } - } -} - -private fun StringBuilder.appendConeType(coneType: ConeKotlinType) { - fun appendClassLikeType(type: ConeClassLikeType) { - append("L") - val classId = type.lookupTag.classId - append(classId.packageFqName.asString().replace(".", "/")) - append("/") - append(classId.relativeClassName) - } - - if (coneType is ConeClassErrorType) return - when (coneType) { - is ConeClassLikeType -> { - appendClassLikeType(coneType) - } - is ConeTypeParameterType -> { - val representative = coneType.lookupTag.typeParameterSymbol.fir.bounds.firstOrNull { - (it as? FirResolvedTypeRef)?.type is ConeClassLikeType - } - if (representative == null) { - append("Ljava/lang/Object") - } else { - appendClassLikeType(representative.coneTypeUnsafe()) - } - append(coneType.lookupTag.name) - } - } - append(";") -} - -private fun FirJavaTypeRef.isVoid(): Boolean { - return type is JavaPrimitiveType && type.type == null -} - - - diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaMethod.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaMethod.kt index 3b177a98934..3b1f1fb4273 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaMethod.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaMethod.kt @@ -12,7 +12,7 @@ import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.impl.FirDeclarationStatusImpl import org.jetbrains.kotlin.fir.declarations.impl.FirSimpleFunctionImpl -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol import org.jetbrains.kotlin.name.Name diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaValueParameter.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaValueParameter.kt index 61e4d6d728a..c903fe43cec 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaValueParameter.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/declarations/FirJavaValueParameter.kt @@ -9,7 +9,7 @@ import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSourceElement import org.jetbrains.kotlin.fir.declarations.FirResolvePhase import org.jetbrains.kotlin.fir.declarations.impl.FirValueParameterImpl -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.fir.symbols.impl.FirVariableSymbol import org.jetbrains.kotlin.name.Name diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt index 2a904092477..0455a381bd2 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/EnhancementSignatureParts.kt @@ -15,7 +15,7 @@ import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack import org.jetbrains.kotlin.fir.java.toConeKotlinTypeWithNullability import org.jetbrains.kotlin.fir.java.toFirJavaTypeRef import org.jetbrains.kotlin.fir.java.toNotNullConeKotlinType -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.load.java.AnnotationTypeQualifierResolver diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt index 506edfcc677..3365daf5b3a 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/enhancement/javaTypeUtils.kt @@ -21,7 +21,7 @@ import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass import org.jetbrains.kotlin.fir.java.declarations.FirJavaField import org.jetbrains.kotlin.fir.java.toConeProjection import org.jetbrains.kotlin.fir.java.toNotNullConeKotlinType -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.fir.references.impl.FirResolvedNamedReferenceImpl import org.jetbrains.kotlin.fir.references.impl.FirSimpleNamedReference import org.jetbrains.kotlin.fir.resolve.* diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt index 76ba4133a53..6a99fac3c72 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassEnhancementScope.kt @@ -13,13 +13,13 @@ import org.jetbrains.kotlin.fir.declarations.impl.* import org.jetbrains.kotlin.fir.expressions.FirExpression import org.jetbrains.kotlin.fir.expressions.impl.FirConstExpressionImpl import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack -import org.jetbrains.kotlin.fir.java.computeJvmDescriptor import org.jetbrains.kotlin.fir.java.declarations.* import org.jetbrains.kotlin.fir.java.enhancement.* -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.fir.render import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.ProcessorAction +import org.jetbrains.kotlin.fir.scopes.jvm.computeJvmDescriptor import org.jetbrains.kotlin.fir.symbols.CallableId import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.FirResolvedTypeRef diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt index 8789c86c531..05c21b44307 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt +++ b/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/scopes/JavaClassUseSiteMemberScope.kt @@ -14,7 +14,6 @@ import org.jetbrains.kotlin.fir.java.JavaTypeParameterStack import org.jetbrains.kotlin.fir.java.declarations.FirJavaClass import org.jetbrains.kotlin.fir.java.declarations.FirJavaMethod import org.jetbrains.kotlin.fir.java.declarations.FirJavaValueParameter -import org.jetbrains.kotlin.fir.java.types.FirJavaTypeRef import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.ProcessorAction import org.jetbrains.kotlin.fir.scopes.ProcessorAction.NEXT @@ -23,6 +22,7 @@ import org.jetbrains.kotlin.fir.scopes.impl.AbstractFirUseSiteMemberScope import org.jetbrains.kotlin.fir.scopes.impl.FirSuperTypeScope import org.jetbrains.kotlin.fir.symbols.CallableId import org.jetbrains.kotlin.fir.symbols.impl.* +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef import org.jetbrains.kotlin.name.Name class JavaClassUseSiteMemberScope( diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirSymbolProvider.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirSymbolProvider.kt index b10e7bc229a..949241589f3 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirSymbolProvider.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/FirSymbolProvider.kt @@ -5,10 +5,13 @@ package org.jetbrains.kotlin.fir.resolve +import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSessionComponent +import org.jetbrains.kotlin.fir.declarations.FirRegularClass import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.fir.scopes.ProcessorAction +import org.jetbrains.kotlin.fir.scopes.jvm.JvmMappedScope import org.jetbrains.kotlin.fir.symbols.* import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol @@ -62,6 +65,27 @@ abstract class FirSymbolProvider : FirSessionComponent { companion object { fun getInstance(session: FirSession) = session.firSymbolProvider + + fun wrapScopeWithJvmMapped( + classId: ClassId, + declaredMemberScope: FirScope, + useSiteSession: FirSession, + scopeSession: ScopeSession + ): FirScope { + val javaClassId = JavaToKotlinClassMap.mapKotlinToJava(classId.asSingleFqName().toUnsafe()) + ?: return declaredMemberScope + val symbolProvider = useSiteSession.firSymbolProvider + val javaClass = symbolProvider.getClassLikeSymbolByFqName(javaClassId)?.fir as? FirRegularClass + ?: return declaredMemberScope + val preparedSignatures = JvmMappedScope.prepareSignatures(javaClass) + return if (preparedSignatures.isNotEmpty()) { + symbolProvider.getClassUseSiteMemberScope(javaClassId, useSiteSession, scopeSession)?.let { + JvmMappedScope(declaredMemberScope, it, preparedSignatures) + } ?: declaredMemberScope + } else { + declaredMemberScope + } + } } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt index 4f68c0bb789..ba72b16e10b 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/SupertypeUtils.kt @@ -73,17 +73,20 @@ fun FirClass<*>.buildUseSiteMemberScope(useSiteSession: FirSession, builder: Sco return symbolProvider.getClassUseSiteMemberScope(classId, useSiteSession, builder) } -fun FirClass<*>.buildDefaultUseSiteMemberScope(useSiteSession: FirSession, builder: ScopeSession): FirScope { - return builder.getOrBuild(symbol, USE_SITE) { +fun FirClass<*>.buildDefaultUseSiteMemberScope(useSiteSession: FirSession, scopeSession: ScopeSession): FirScope { + return scopeSession.getOrBuild(symbol, USE_SITE) { val declaredScope = declaredMemberScope(this) + val wrappedDeclaredScope = FirSymbolProvider.wrapScopeWithJvmMapped( + classId, declaredScope, useSiteSession, scopeSession + ) val scopes = lookupSuperTypes(this, lookupInterfaces = true, deep = false, useSiteSession = useSiteSession) .mapNotNull { useSiteSuperType -> if (useSiteSuperType is ConeClassErrorType) return@mapNotNull null val symbol = useSiteSuperType.lookupTag.toSymbol(useSiteSession) if (symbol is FirRegularClassSymbol) { - val useSiteMemberScope = symbol.fir.buildUseSiteMemberScope(useSiteSession, builder)!! - useSiteSuperType.wrapSubstitutionScopeIfNeed(useSiteSession, useSiteMemberScope, symbol.fir, builder) + val useSiteMemberScope = symbol.fir.buildUseSiteMemberScope(useSiteSession, scopeSession)!! + useSiteSuperType.wrapSubstitutionScopeIfNeed(useSiteSession, useSiteMemberScope, symbol.fir, scopeSession) } else { null } @@ -91,7 +94,7 @@ fun FirClass<*>.buildDefaultUseSiteMemberScope(useSiteSession: FirSession, build FirClassUseSiteMemberScope( useSiteSession, FirSuperTypeScope(useSiteSession, FirStandardOverrideChecker(useSiteSession), scopes), - declaredScope + wrappedDeclaredScope ) } } diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirMemberScopeProvider.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirMemberScopeProvider.kt index 2dc6cd868a3..02a957a0784 100644 --- a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirMemberScopeProvider.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/impl/FirMemberScopeProvider.kt @@ -9,13 +9,14 @@ import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.FirSessionComponent import org.jetbrains.kotlin.fir.declarations.FirClass import org.jetbrains.kotlin.fir.resolve.memberScopeProvider +import org.jetbrains.kotlin.fir.scopes.FirScope import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name class FirMemberScopeProvider : FirSessionComponent { - private val declaredMemberCache = mutableMapOf, FirClassDeclaredMemberScope>() + private val declaredMemberCache = mutableMapOf, FirScope>() private val nestedClassifierCache = mutableMapOf, FirNestedClassifierScope>() private val selfImportingCache = mutableMapOf() @@ -23,7 +24,7 @@ class FirMemberScopeProvider : FirSessionComponent { klass: FirClass<*>, useLazyNestedClassifierScope: Boolean, existingNames: List? - ): FirClassDeclaredMemberScope { + ): FirScope { return declaredMemberCache.getOrPut(klass) { FirClassDeclaredMemberScope(klass, useLazyNestedClassifierScope, existingNames) } @@ -47,7 +48,7 @@ fun declaredMemberScope( klass: FirClass<*>, useLazyNestedClassifierScope: Boolean = false, existingNames: List? = null -): FirClassDeclaredMemberScope { +): FirScope { return klass .session .memberScopeProvider diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt new file mode 100644 index 00000000000..4b11fd4436d --- /dev/null +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/DescriptorUtils.kt @@ -0,0 +1,114 @@ +/* + * 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.fir.scopes.jvm + +import org.jetbrains.kotlin.fir.declarations.FirFunction +import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction +import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.jvm.FirJavaTypeRef +import org.jetbrains.kotlin.load.java.structure.JavaClass +import org.jetbrains.kotlin.load.java.structure.JavaClassifierType +import org.jetbrains.kotlin.load.java.structure.JavaPrimitiveType +import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter +import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName + +fun FirFunction<*>.computeJvmDescriptor(): String = buildString { + if (this@computeJvmDescriptor is FirSimpleFunction) { + append(name.asString()) + } else { + append("") + } + + append("(") + for (parameter in valueParameters) { + appendErasedType(parameter.returnTypeRef) + } + append(")") + + if (this@computeJvmDescriptor !is FirSimpleFunction || returnTypeRef.isVoid()) { + append("V") + } else { + appendErasedType(returnTypeRef) + } +} + +// TODO: primitive types, arrays, etc. +private fun StringBuilder.appendErasedType(typeRef: FirTypeRef) { + fun appendClass(klass: JavaClass) { + klass.fqName?.let { + append("L") + append(it.asString().replace(".", "/")) + } + } + + when (typeRef) { + is FirResolvedTypeRef -> appendConeType(typeRef.type) + is FirJavaTypeRef -> { + when (val javaType = typeRef.type) { + is JavaClassifierType -> { + when (val classifier = javaType.classifier) { + is JavaClass -> appendClass(classifier) + is JavaTypeParameter -> { + val representative = classifier.upperBounds.firstOrNull { it.classifier is JavaClass } + if (representative == null) { + append("Ljava/lang/Object") + } else { + appendClass(representative.classifier as JavaClass) + } + } + else -> return + } + append(";") + } + } + } + } +} + +private fun StringBuilder.appendConeType(coneType: ConeKotlinType) { + fun appendClassLikeType(type: ConeClassLikeType) { + append("L") + val classId = type.lookupTag.classId + append(classId.packageFqName.asString().replace(".", "/")) + append("/") + append(classId.relativeClassName) + } + + if (coneType is ConeClassErrorType) return + when (coneType) { + is ConeClassLikeType -> { + appendClassLikeType(coneType) + } + is ConeTypeParameterType -> { + val representative = coneType.lookupTag.typeParameterSymbol.fir.bounds.firstOrNull { + (it as? FirResolvedTypeRef)?.type is ConeClassLikeType + } + if (representative == null) { + append("Ljava/lang/Object") + } else { + appendClassLikeType(representative.coneTypeUnsafe()) + } + append(coneType.lookupTag.name) + } + } + append(";") +} + +private val unitClassId = ClassId.topLevel(FqName("kotlin.Unit")) + +private fun FirTypeRef.isVoid(): Boolean { + return when (this) { + is FirJavaTypeRef -> { + type is JavaPrimitiveType && type.type == null + } + is FirResolvedTypeRef -> { + val type = type + type is ConeClassLikeType && type.lookupTag.classId == unitClassId + } + else -> false + } +} diff --git a/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt new file mode 100644 index 00000000000..c6e070108cf --- /dev/null +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/scopes/jvm/JvmMappedScope.kt @@ -0,0 +1,54 @@ +/* + * 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.fir.scopes.jvm + +import org.jetbrains.kotlin.builtins.jvm.JvmBuiltInsSettings +import org.jetbrains.kotlin.fir.declarations.FirRegularClass +import org.jetbrains.kotlin.fir.scopes.FirScope +import org.jetbrains.kotlin.fir.scopes.ProcessorAction +import org.jetbrains.kotlin.fir.symbols.impl.FirCallableSymbol +import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol +import org.jetbrains.kotlin.name.Name + +class JvmMappedScope( + private val declaredMemberScope: FirScope, + private val javaMappedScope: FirScope, + private val whiteListSignaturesByName: Map> +) : FirScope() { + + override fun processFunctionsByName(name: Name, processor: (FirFunctionSymbol<*>) -> ProcessorAction): ProcessorAction { + val whiteListSignatures = whiteListSignaturesByName[name] + ?: return declaredMemberScope.processFunctionsByName(name, processor) + if (!javaMappedScope.processFunctionsByName(name) { symbol -> + val jvmSignature = symbol.fir.computeJvmDescriptor() + if (jvmSignature !in whiteListSignatures) { + ProcessorAction.NEXT + } else { + processor(symbol) + } + } + ) return ProcessorAction.STOP + + return declaredMemberScope.processFunctionsByName(name, processor) + } + + override fun processPropertiesByName(name: Name, processor: (FirCallableSymbol<*>) -> ProcessorAction): ProcessorAction { + return declaredMemberScope.processPropertiesByName(name, processor) + } + + companion object { + fun prepareSignatures(klass: FirRegularClass): Map> { + val signaturePrefix = klass.symbol.classId.toString() + val filteredSignatures = JvmBuiltInsSettings.WHITE_LIST_METHOD_SIGNATURES.filter { signature -> + signature.startsWith(signaturePrefix) + }.map { signature -> + // +1 to delete dot before function name + signature.substring(signaturePrefix.length + 1) + } + return filteredSignatures.groupBy { Name.identifier(it.substringBefore("(")) } + } + } +} \ No newline at end of file diff --git a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/types/FirJavaTypeRef.kt b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/jvm/FirJavaTypeRef.kt similarity index 93% rename from compiler/fir/java/src/org/jetbrains/kotlin/fir/java/types/FirJavaTypeRef.kt rename to compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/jvm/FirJavaTypeRef.kt index 8be15eeb874..e6b793b4e43 100644 --- a/compiler/fir/java/src/org/jetbrains/kotlin/fir/java/types/FirJavaTypeRef.kt +++ b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/types/jvm/FirJavaTypeRef.kt @@ -3,7 +3,7 @@ * 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.fir.java.types +package org.jetbrains.kotlin.fir.types.jvm import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.types.impl.FirUserTypeRefImpl diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt index dad4eeb6c3d..8aafa5e5843 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyException.txt @@ -6,17 +6,17 @@ FILE: test.kt } public final fun test(e: R|MyException|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { - R|/e|.R|kotlin/printStackTrace|() + R|/e|.R|java/lang/Throwable.printStackTrace|() R|/e|.R|kotlin/printStackTrace|(R|/stream|) lval result: = R|/e|.#() } public final fun test(e: R|YourException|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { - R|/e|.R|kotlin/printStackTrace|() + R|/e|.R|java/lang/Throwable.printStackTrace|() R|/e|.R|kotlin/printStackTrace|(R|/stream|) lval result: = R|/e|.#() } public final fun test(e: R|kotlin/Exception|, stream: R|java/io/PrintStream|): R|kotlin/Unit| { - R|/e|.R|kotlin/printStackTrace|() + R|/e|.R|java/lang/Throwable.printStackTrace|() R|/e|.R|kotlin/printStackTrace|(R|/stream|) lval result: = R|/e|.#() } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.kt index bd7fd4d54fc..b41a527a260 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.kt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.kt @@ -6,11 +6,11 @@ public interface MyIterable extends Iterable interface UseIterable : MyIterable { fun test() { val it = iterator() - val split = spliterator() + val split = spliterator() } } fun test(some: Iterable) { val it = some.iterator() - val split = some.spliterator() + val split = some.spliterator() } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.txt index b10f1123687..5c57df8dcd9 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyIterable.txt @@ -2,11 +2,11 @@ FILE: test.kt public abstract interface UseIterable : R|MyIterable| { public open fun test(): R|kotlin/Unit| { lval it: R|kotlin/collections/MutableIterator| = this@R|kotlin/collections/MutableIterable|.R|FakeOverride|>|() - lval split: = #() + lval split: R|java/util/Spliterator| = this@R|java/lang/Iterable|.R|FakeOverride|>|() } } public final fun test(some: R|kotlin/collections/Iterable|): R|kotlin/Unit| { lval it: R|kotlin/collections/Iterator| = R|/some|.R|FakeOverride|>|() - lval split: = R|/some|.#() + lval split: R|java/util/Spliterator| = R|/some|.R|FakeOverride|>|() } diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt index f3e72aaece3..f7e178a36fc 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.kt @@ -10,7 +10,7 @@ fun test(map: MyMap) { val otherResult = map.getOrDefault("key", "value") val anotherResult = map.replace("key", "value") // Java forEach - map.forEach { key, value -> + map.forEach { key, value -> println("$key: $value") } // Kotlin forEach @@ -25,7 +25,7 @@ fun test(map: MutableMap) { val otherResult = map.getOrDefault("key", "value") val anotherResult = map.replace("key", "value") // Java forEach - map.forEach { key, value -> + map.forEach { key, value -> println("$key: $value") } // Kotlin forEach diff --git a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt index edb5b9ce067..c68e14fd8e2 100644 --- a/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt +++ b/compiler/fir/resolve/testData/resolve/stdlib/j+k/MyMap.txt @@ -6,8 +6,8 @@ FILE: test.kt ) lval otherResult: R|kotlin/String| = R|/map|.R|FakeOverride|(String(key), String(value)) lval anotherResult: = R|/map|.#(String(key), String(value)) - R|/map|.#( = forEach@fun (key: R|class error: No type for parameter|, value: R|class error: No type for parameter|): R|kotlin/Unit| { - R|kotlin/io/println|((R|/key|.R|kotlin/toString|(), String(: ), R|/value|.R|kotlin/toString|())) + R|/map|.R|java/util/Map.forEach|( = forEach@fun (key: R|K!|, value: R|V!|): R|kotlin/Unit| { + R|kotlin/io/println|((R|/key|.R|kotlin/Any.toString|(), String(: ), R|/value|.R|kotlin/Any.toString|())) } ) R|/map|.R|kotlin/collections/forEach|( = forEach@fun (: R|kotlin/collections/Map.Entry|): R|kotlin/Unit| { @@ -24,8 +24,8 @@ FILE: test.kt ) lval otherResult: R|kotlin/String| = R|/map|.R|FakeOverride|(String(key), String(value)) lval anotherResult: = R|/map|.#(String(key), String(value)) - R|/map|.#( = forEach@fun (key: R|class error: No type for parameter|, value: R|class error: No type for parameter|): R|kotlin/Unit| { - R|kotlin/io/println|((R|/key|.R|kotlin/toString|(), String(: ), R|/value|.R|kotlin/toString|())) + R|/map|.R|java/util/Map.forEach|( = forEach@fun (key: R|K!|, value: R|V!|): R|kotlin/Unit| { + R|kotlin/io/println|((R|/key|.R|kotlin/Any.toString|(), String(: ), R|/value|.R|kotlin/Any.toString|())) } ) R|/map|.R|kotlin/collections/forEach|( = forEach@fun (: R|kotlin/collections/Map.Entry|): R|kotlin/Unit| {