diff --git a/compiler/fir/entrypoint/build.gradle.kts b/compiler/fir/entrypoint/build.gradle.kts index 72911e081c2..61890688552 100644 --- a/compiler/fir/entrypoint/build.gradle.kts +++ b/compiler/fir/entrypoint/build.gradle.kts @@ -16,6 +16,7 @@ dependencies { api(project(":compiler:fir:checkers:checkers.native")) api(project(":js:js.frontend")) + implementation(project(":core:compiler.common.native")) implementation(project(":compiler:fir:resolve")) implementation(project(":compiler:fir:fir-serialization")) implementation(project(":compiler:fir:fir2ir:jvm-backend")) diff --git a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/NativeForwardDeclarationsSymbolProvider.kt b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/NativeForwardDeclarationsSymbolProvider.kt index ee984e25508..28d1f2f04ac 100644 --- a/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/NativeForwardDeclarationsSymbolProvider.kt +++ b/compiler/fir/entrypoint/src/org/jetbrains/kotlin/fir/session/NativeForwardDeclarationsSymbolProvider.kt @@ -29,7 +29,7 @@ import org.jetbrains.kotlin.fir.types.constructClassType import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.includedForwardDeclarations import org.jetbrains.kotlin.library.isInterop -import org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationKind +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name @@ -41,7 +41,7 @@ class NativeForwardDeclarationsSymbolProvider( private val kotlinLibraries: Collection, ) : FirSymbolProvider(session) { private companion object { - private val validPackages = ForwardDeclarationKind.packageFqNameToKind.keys + private val validPackages = NativeForwardDeclarationKind.packageFqNameToKind.keys } private val includedForwardDeclarations: Set by lazy { @@ -77,7 +77,7 @@ class NativeForwardDeclarationsSymbolProvider( session.firCachesFactory.createCache(::createSyntheticForwardDeclarationClass) private fun createSyntheticForwardDeclarationClass(classId: ClassId): FirClassLikeSymbol<*>? { - val forwardDeclarationKind = ForwardDeclarationKind.packageFqNameToKind[classId.packageFqName] ?: return null + val forwardDeclarationKind = NativeForwardDeclarationKind.packageFqNameToKind[classId.packageFqName] ?: return null val symbol = FirRegularClassSymbol(classId) diff --git a/compiler/util-klib-metadata/build.gradle.kts b/compiler/util-klib-metadata/build.gradle.kts index d15c8960bb0..588f71a7e98 100644 --- a/compiler/util-klib-metadata/build.gradle.kts +++ b/compiler/util-klib-metadata/build.gradle.kts @@ -9,6 +9,7 @@ dependencies { compileOnly(project(":compiler:cli-common")) compileOnly(project(":compiler:frontend")) compileOnly(project(":core:deserialization")) + compileOnly(project(":core:compiler.common.native")) compileOnly(project(":compiler:serialization")) api(kotlinStdlib()) diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/NativeTypeTransformer.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/NativeTypeTransformer.kt index 349d2cb8555..dfbbecacd8a 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/NativeTypeTransformer.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/NativeTypeTransformer.kt @@ -9,8 +9,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.descriptors.deserialization.PlatformDependentTypeTransformer import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.types.SimpleType import org.jetbrains.kotlin.types.SupposititiousSimpleType @@ -31,13 +30,6 @@ class NativeTypeTransformer : PlatformDependentTypeTransformer { } companion object { - private val cNames = FqName("cnames") - private val cNamesStructs = cNames.child(Name.identifier("structs")) - - private val objCNames = FqName("objcnames") - private val objCNamesClasses = objCNames.child(Name.identifier("classes")) - private val objCNamesProtocols = objCNames.child(Name.identifier("protocols")) - - private val forwardPackagesSet = setOf(cNamesStructs, objCNamesClasses, objCNamesProtocols) + private val forwardPackagesSet = NativeForwardDeclarationKind.entries.map { it.packageFqName }.toSet() } } \ No newline at end of file diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataDeserializedPackageFragmentsFactoryImpl.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataDeserializedPackageFragmentsFactoryImpl.kt index df7d840ab05..0285b587b59 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataDeserializedPackageFragmentsFactoryImpl.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataDeserializedPackageFragmentsFactoryImpl.kt @@ -8,6 +8,7 @@ import org.jetbrains.kotlin.library.exportForwardDeclarations import org.jetbrains.kotlin.library.isInterop import org.jetbrains.kotlin.library.metadata.* import org.jetbrains.kotlin.library.packageFqName +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe @@ -138,7 +139,7 @@ class ClassifierAliasingPackageFragmentDescriptor( targets: List, module: ModuleDescriptor, private val checker: ExportedForwardDeclarationChecker, -) : PackageFragmentDescriptorImpl(module, checker.fqName) { +) : PackageFragmentDescriptorImpl(module, checker.declKind.packageFqName) { private val memberScope = object : MemberScopeImpl() { override fun getContributedClassifier(name: Name, location: LookupLocation) = targets.firstNotNullOfOrNull { @@ -169,43 +170,14 @@ class ClassifierAliasingPackageFragmentDescriptor( * we need to check declaration type before returning the result of lookup. * See KT-49034. */ -enum class ExportedForwardDeclarationChecker(val fqName: FqName) { +enum class ExportedForwardDeclarationChecker(val declKind: NativeForwardDeclarationKind) { - Struct(ForwardDeclarationsFqNames.cNamesStructs) { - override fun check(classifierDescriptor: ClassifierDescriptor): Boolean = - classifierDescriptor is ClassDescriptor && classifierDescriptor.kind.isClass && - classifierDescriptor.isCStructVar() - - }, - ObjCClass(ForwardDeclarationsFqNames.objCNamesClasses) { - override fun check(classifierDescriptor: ClassifierDescriptor): Boolean = - classifierDescriptor is ClassDescriptor && classifierDescriptor.kind.isClass && - classifierDescriptor.isObjCObjectBase() - }, - ObjCProtocol(ForwardDeclarationsFqNames.objCNamesProtocols) { - override fun check(classifierDescriptor: ClassifierDescriptor): Boolean = - classifierDescriptor is ClassDescriptor && classifierDescriptor.kind.isInterface && - classifierDescriptor.isObjCObject() - } + Struct(NativeForwardDeclarationKind.Struct), + ObjCClass(NativeForwardDeclarationKind.ObjCClass), + ObjCProtocol(NativeForwardDeclarationKind.ObjCProtocol) ; - abstract fun check(classifierDescriptor: ClassifierDescriptor): Boolean - - companion object { - private val cStructVarFqName = FqName("kotlinx.cinterop.CStructVar") - private val objCObjectBaseFqName = FqName("kotlinx.cinterop.ObjCObjectBase") - private val objCObjectFqName = FqName("kotlinx.cinterop.ObjCObject") - - // We can stop at ObjCObjectBase when checking Obj-C classes. - // Checking @ExternalObjCClass would be faster, but this annotation is not commonized. See KT-57541. - private fun ClassifierDescriptor.isObjCObjectBase(): Boolean = - getAllSuperClassifiers().any { it.fqNameSafe == objCObjectBaseFqName } - - // For protocols, we have to go all the way up to ObjCObject. - private fun ClassifierDescriptor.isObjCObject(): Boolean = - getAllSuperClassifiers().any { it.fqNameSafe == objCObjectFqName } - - private fun ClassifierDescriptor.isCStructVar(): Boolean = - getAllSuperClassifiers().any { it.fqNameSafe == cStructVarFqName } - } + fun check(classifierDescriptor: ClassifierDescriptor): Boolean = classifierDescriptor is ClassDescriptor && + classifierDescriptor.kind == declKind.classKind && + classifierDescriptor.getAllSuperClassifiers().any { it.fqNameSafe == declKind.matchSuperClassFqName } } diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataModuleDescriptorFactoryImpl.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataModuleDescriptorFactoryImpl.kt index dbf1ad920eb..03d1b318fa2 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataModuleDescriptorFactoryImpl.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibMetadataModuleDescriptorFactoryImpl.kt @@ -18,7 +18,7 @@ import org.jetbrains.kotlin.incremental.components.LookupTracker import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.metadata.* import org.jetbrains.kotlin.library.unresolvedDependencies -import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.parentOrNull import org.jetbrains.kotlin.platform.jvm.isJvm @@ -194,22 +194,18 @@ class KlibMetadataModuleDescriptorFactoryImpl( storageManager: StorageManager, module: ModuleDescriptorImpl ): PackageFragmentProviderImpl { - fun createPackage(fqName: FqName, supertypeName: String, classKind: ClassKind) = + fun createPackage(kind: NativeForwardDeclarationKind) = ForwardDeclarationsPackageFragmentDescriptor( storageManager, module, - fqName, - Name.identifier(supertypeName), - classKind, + kind.packageFqName, + kind.superClassName, + kind.classKind, isExpect = true ) val packageFragmentProvider = PackageFragmentProviderImpl( - listOf( - createPackage(ForwardDeclarationsFqNames.cNamesStructs, "COpaque", ClassKind.CLASS), - createPackage(ForwardDeclarationsFqNames.objCNamesClasses, "ObjCObjectBase", ClassKind.CLASS), - createPackage(ForwardDeclarationsFqNames.objCNamesProtocols, "ObjCObject", ClassKind.INTERFACE) - ) + NativeForwardDeclarationKind.entries.map { createPackage(it) } ) return packageFragmentProvider } diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibResolvedModuleDescriptorsFactoryImpl.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibResolvedModuleDescriptorsFactoryImpl.kt index b5cc8c4620b..60d2ebe3ebb 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibResolvedModuleDescriptorsFactoryImpl.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/library/metadata/impl/KlibResolvedModuleDescriptorsFactoryImpl.kt @@ -17,9 +17,7 @@ import org.jetbrains.kotlin.konan.file.File import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.metadata.* import org.jetbrains.kotlin.library.metadata.resolver.KotlinLibraryResolveResult -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.* import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl @@ -121,18 +119,18 @@ class KlibResolvedModuleDescriptorsFactoryImpl( val module = createDescriptorOptionalBuiltsIns(FORWARD_DECLARATIONS_MODULE_NAME, storageManager, builtIns, SyntheticModulesOrigin) - fun createPackage(forwardDeclarationKind: ForwardDeclarationKind) = + fun createPackage(forwardDeclarationKind: NativeForwardDeclarationKind) = ForwardDeclarationsPackageFragmentDescriptor( storageManager, module, forwardDeclarationKind.packageFqName, - Name.identifier(forwardDeclarationKind.superClassName), + forwardDeclarationKind.superClassName, forwardDeclarationKind.classKind, isExpect ) val packageFragmentProvider = PackageFragmentProviderImpl( - ForwardDeclarationKind.values().map { createPackage(it) } + NativeForwardDeclarationKind.entries.map { createPackage(it) } ) module.initialize(packageFragmentProvider) @@ -184,7 +182,7 @@ class ForwardDeclarationsPackageFragmentDescriptor( private val declarations = storageManager.createMemoizedFunction(this::createDeclaration) private val supertype by storageManager.createLazyValue { - val descriptor = builtIns.builtInsModule.getPackage(ForwardDeclarationsFqNames.cInterop) + val descriptor = builtIns.builtInsModule.getPackage(NativeStandardInteropNames.cInteropPackage) .memberScope .getContributedClassifier(supertypeName, NoLookupLocation.FROM_BACKEND) as ClassDescriptor @@ -218,31 +216,18 @@ class ForwardDeclarationsPackageFragmentDescriptor( override fun getMemberScope(): MemberScope = memberScope } -// TODO decouple and move interop-specific logic back to Kotlin/Native. + +@Deprecated( + level = DeprecationLevel.ERROR, + message = "This class was moved to org.jetbrains.kotlin.name.NativeStandardInteropNames.ForwardDeclarations", +) object ForwardDeclarationsFqNames { - internal val cInterop = FqName("kotlinx.cinterop") + internal val cInterop = NativeStandardInteropNames.cInteropPackage - private val cNames = FqName("cnames") - internal val cNamesStructs = cNames.child(Name.identifier("structs")) + internal val cNamesStructs = NativeStandardInteropNames.ForwardDeclarations.cNamesStructsPackage + internal val objCNamesClasses = NativeStandardInteropNames.ForwardDeclarations.objCNamesClassesPackage + internal val objCNamesProtocols = NativeStandardInteropNames.ForwardDeclarations.objCNamesProtocolsPackage - private val objCNames = FqName("objcnames") - internal val objCNamesClasses = objCNames.child(Name.identifier("classes")) - internal val objCNamesProtocols = objCNames.child(Name.identifier("protocols")) - - val syntheticPackages = setOf(cNames, objCNames) -} - -enum class ForwardDeclarationKind(val packageFqName: FqName, val superClassName: String, val classKind: ClassKind) { - CNAMES_STRUCTS(ForwardDeclarationsFqNames.cNamesStructs, "COpaque", ClassKind.CLASS), - OBJCNAMES_CLASSES(ForwardDeclarationsFqNames.objCNamesClasses, "ObjCObjectBase", ClassKind.CLASS), - OBJCNAMES_PROTOCOLS(ForwardDeclarationsFqNames.objCNamesProtocols, "ObjCObject", ClassKind.INTERFACE) - - ; - - val superClassId = ClassId.topLevel(ForwardDeclarationsFqNames.cInterop.child(Name.identifier(superClassName))) - - companion object { - val packageFqNameToKind: Map = ForwardDeclarationKind.values().associateBy { it.packageFqName } - } + val syntheticPackages = NativeStandardInteropNames.ForwardDeclarations.syntheticPackages } \ No newline at end of file diff --git a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/serialization/konan/impl/Deprecated.kt b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/serialization/konan/impl/Deprecated.kt index db72bc63be4..4cf7fd23482 100644 --- a/compiler/util-klib-metadata/src/org/jetbrains/kotlin/serialization/konan/impl/Deprecated.kt +++ b/compiler/util-klib-metadata/src/org/jetbrains/kotlin/serialization/konan/impl/Deprecated.kt @@ -29,4 +29,5 @@ typealias ForwardDeclarationsPackageFragmentDescriptor = org.jetbrains.kotlin.li "This object has been moved from package org.jetbrains.kotlin.serialization.konan.impl to package org.jetbrains.kotlin.library.metadata.impl", ReplaceWith("org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationsFqNames") ) +@Suppress("DEPRECATION_ERROR") typealias ForwardDeclarationsFqNames = org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationsFqNames diff --git a/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeForwardDeclarationKind.kt b/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeForwardDeclarationKind.kt new file mode 100644 index 00000000000..ac23c979e4c --- /dev/null +++ b/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeForwardDeclarationKind.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2023 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.name + +import org.jetbrains.kotlin.descriptors.ClassKind + +/** + * Enum class representing different kinds of synthetic forward declarations. + * + * @property packageFqName The fully qualified name of the package where the forward declarations of this kind are located. + * @property superClassName The name of the generated superclass of the forward declaration. + * @property matchSuperClassName The name of the superclass real declaration corresponding to this synthetic should have. + * @property classKind The class kind of the forward declaration class (class or interface). + * + * Also, fqName and classId of super classes are stored as optimization to avoid allocations and string processing on usage. + */ +enum class NativeForwardDeclarationKind(val packageFqName: FqName, val superClassName: Name, val matchSuperClassName: Name, val classKind: ClassKind) { + Struct( + NativeStandardInteropNames.ForwardDeclarations.cNamesStructsPackage, + NativeStandardInteropNames.COpaque, + NativeStandardInteropNames.CStructVar, + ClassKind.CLASS + ), + ObjCClass( + NativeStandardInteropNames.ForwardDeclarations.objCNamesClassesPackage, + NativeStandardInteropNames.ObjCObjectBase, + NativeStandardInteropNames.ObjCObjectBase, + ClassKind.CLASS + ), + ObjCProtocol( + NativeStandardInteropNames.ForwardDeclarations.objCNamesProtocolsPackage, + NativeStandardInteropNames.ObjCObject, + NativeStandardInteropNames.ObjCObject, + ClassKind.INTERFACE + ) + ; + + val superClassFqName = NativeStandardInteropNames.cInteropPackage.child(superClassName) + val matchSuperClassFqName = NativeStandardInteropNames.cInteropPackage.child(matchSuperClassName) + val superClassId = ClassId.topLevel(superClassFqName) + val matchSuperClassId = ClassId.topLevel(matchSuperClassFqName) + + companion object { + val packageFqNameToKind: Map = NativeForwardDeclarationKind.entries.associateBy { it.packageFqName } + } +} \ No newline at end of file diff --git a/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeStandardInteropNames.kt b/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeStandardInteropNames.kt new file mode 100644 index 00000000000..9c5e36c512f --- /dev/null +++ b/core/compiler.common.native/src/org/jetbrains/kotlin/name/NativeStandardInteropNames.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2010-2023 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.name + +object NativeStandardInteropNames { + val cInteropPackage = FqName("kotlinx.cinterop") + + internal val COpaque = Name.identifier("COpaque") + internal val CStructVar = Name.identifier("CStructVar") + internal val ObjCObjectBase = Name.identifier("ObjCObjectBase") + internal val ObjCObject = Name.identifier("ObjCObject") + + object ForwardDeclarations { + private val cNamesPackage = FqName("cnames") + val cNamesStructsPackage = cNamesPackage.child(Name.identifier("structs")) + + private val objCNamesPackage = FqName("objcnames") + val objCNamesClassesPackage = objCNamesPackage.child(Name.identifier("classes")) + val objCNamesProtocolsPackage = objCNamesPackage.child(Name.identifier("protocols")) + + val syntheticPackages = setOf(cNamesPackage, objCNamesPackage) + } +} \ No newline at end of file diff --git a/kotlin-native/backend.native/build.gradle b/kotlin-native/backend.native/build.gradle index 7b37e923b36..bd446a1b9a3 100644 --- a/kotlin-native/backend.native/build.gradle +++ b/kotlin-native/backend.native/build.gradle @@ -157,6 +157,7 @@ dependencies { compilerImplementation project(":kotlin-compiler") compilerApi project(":native:kotlin-native-utils") compilerApi project(":core:descriptors") + compilerApi project(":core:compiler.common.native") compilerApi project(":compiler:ir.tree") compilerApi project(":compiler:ir.backend.common") compilerApi project(":compiler:util") diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Fir2Ir.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Fir2Ir.kt index 456b4123efd..31f0303998b 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Fir2Ir.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Fir2Ir.kt @@ -35,8 +35,8 @@ import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.library.metadata.KlibMetadataFactories -import org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationKind import org.jetbrains.kotlin.storage.LockBasedStorageManager +import org.jetbrains.kotlin.name.* internal val KlibFactories = KlibMetadataFactories(::KonanBuiltIns, DynamicTypeDeserializer, PlatformDependentTypeTransformer.None) @@ -111,7 +111,7 @@ internal fun PhaseContext.fir2Ir( } // This packages exists in all platform libraries, but can contain only synthetic declarations. // These declarations are not really located in klib, so we don't need to depend on klib to use them. - removeAll(ForwardDeclarationKind.values().map { it.packageFqName }) + removeAll(NativeForwardDeclarationKind.entries.map { it.packageFqName }) }.toList() diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt index fd84a7ddc21..f9e0a3d8b0d 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt @@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.descriptors.IrBasedClassConstructorDescriptor import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.util.* +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.ExternalOverridabilityCondition @@ -37,7 +38,6 @@ internal val objCDirectFqName = interopPackageName.child(Name.identifier("ObjCDi internal val objCMethodFqName = interopPackageName.child(Name.identifier("ObjCMethod")) internal val objCConstructorFqName = FqName("kotlinx.cinterop.ObjCConstructor") internal val objCFactoryFqName = interopPackageName.child(Name.identifier("ObjCFactory")) -private val objcnamesForwardDeclarationsPackageName = Name.identifier("objcnames") fun ClassDescriptor.isObjCClass(): Boolean = this.containingDeclaration.fqNameSafe != interopPackageName && @@ -64,8 +64,10 @@ fun IrClass.isExternalObjCClass(): Boolean = this.isObjCClass() && it.annotations.hasAnnotation(externalObjCClassFqName) } -fun ClassDescriptor.isObjCForwardDeclaration(): Boolean = - this.findPackage().fqName.startsWith(objcnamesForwardDeclarationsPackageName) +fun ClassDescriptor.isObjCForwardDeclaration(): Boolean = when (NativeForwardDeclarationKind.packageFqNameToKind[findPackage().fqName]) { + null, NativeForwardDeclarationKind.Struct -> false + NativeForwardDeclarationKind.ObjCProtocol, NativeForwardDeclarationKind.ObjCClass -> true +} fun IrClass.isObjCForwardDeclaration(): Boolean = getPackageFragment().packageFqName.startsWith(objcnamesForwardDeclarationsPackageName) diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIrlinker.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIrlinker.kt index fb573249851..e7d8a8f285f 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIrlinker.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/KonanIrlinker.kt @@ -33,7 +33,6 @@ import org.jetbrains.kotlin.backend.konan.descriptors.isFromInteropLibrary import org.jetbrains.kotlin.backend.konan.descriptors.isInteropLibrary import org.jetbrains.kotlin.backend.konan.ir.interop.IrProviderForCEnumAndCStructStubs import org.jetbrains.kotlin.backend.konan.ir.konanLibrary -import org.jetbrains.kotlin.backend.konan.ir.isFromInteropLibrary import org.jetbrains.kotlin.backend.konan.ir.isFromInteropLibraryByDescriptor import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.konan.isNativeStdlib @@ -58,6 +57,7 @@ import org.jetbrains.kotlin.library.KotlinLibrary import org.jetbrains.kotlin.library.metadata.DeserializedKlibModuleOrigin import org.jetbrains.kotlin.library.metadata.klibModuleOrigin import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.NativeForwardDeclarationKind import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.descriptorUtil.module @@ -472,12 +472,7 @@ internal class KonanIrLinker( ) : KotlinIrLinker(currentModule, messageLogger, builtIns, symbolTable, exportedDependencies) { companion object { - private val C_NAMES_NAME = Name.identifier("cnames") - private val OBJC_NAMES_NAME = Name.identifier("objcnames") - val FORWARD_DECLARATION_ORIGIN = object : IrDeclarationOriginImpl("FORWARD_DECLARATION_ORIGIN") {} - - const val offset = SYNTHETIC_OFFSET } override fun isBuiltInModule(moduleDescriptor: ModuleDescriptor): Boolean = moduleDescriptor.isNativeStdlib() @@ -1111,9 +1106,7 @@ internal class KonanIrLinker( private fun IdSignature.isForwardDeclarationSignature(): Boolean { if (isPubliclyVisible) { - return packageFqName().run { - startsWith(C_NAMES_NAME) || startsWith(OBJC_NAMES_NAME) - } + return packageFqName() in NativeForwardDeclarationKind.packageFqNameToKind } return false diff --git a/native/commonizer/build.gradle.kts b/native/commonizer/build.gradle.kts index 8aa46772fef..9aa88c2770b 100644 --- a/native/commonizer/build.gradle.kts +++ b/native/commonizer/build.gradle.kts @@ -27,6 +27,7 @@ dependencies { compileOnly(project(":compiler:cli-common")) compileOnly(project(":compiler:ir.serialization.common")) compileOnly(project(":compiler:frontend")) + compileOnly(project(":core:compiler.common.native")) compileOnly(project(":native:frontend.native")) compileOnly(project(":kotlin-util-klib-metadata")) compileOnly(intellijCore()) diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/utils/names.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/utils/names.kt index 21b99ca8cb5..d71b69557bb 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/utils/names.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/utils/names.kt @@ -10,10 +10,7 @@ import org.jetbrains.kotlin.builtins.StandardNames import org.jetbrains.kotlin.commonizer.cir.CirEntityId import org.jetbrains.kotlin.commonizer.cir.CirName import org.jetbrains.kotlin.commonizer.cir.CirPackageName -import org.jetbrains.kotlin.library.metadata.impl.ForwardDeclarationsFqNames -import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.* internal val DEPRECATED_ANNOTATION_FQN: FqName = FqName(Deprecated::class.java.name) internal const val DEPRECATED_ANNOTATION_FULL_NAME: ClassName = "kotlin/Deprecated" @@ -38,19 +35,19 @@ private val STANDARD_KOTLIN_PACKAGES: List = listOf( CirPackageName.create("kotlinx") ) -private val KOTLIN_NATIVE_SYNTHETIC_PACKAGES: List = ForwardDeclarationsFqNames.syntheticPackages +private val KOTLIN_NATIVE_SYNTHETIC_PACKAGES: List = NativeStandardInteropNames.ForwardDeclarations.syntheticPackages .map { packageFqName -> check(!packageFqName.isRoot) CirPackageName.create(packageFqName) } -internal val CNAMES_STRUCTS_PACKAGE = CirPackageName.create("cnames.structs") +internal val CNAMES_STRUCTS_PACKAGE = CirPackageName.create(NativeForwardDeclarationKind.Struct.packageFqName) -internal val OBJCNAMES_CLASSES_PACKAGE = CirPackageName.create("objcnames.classes") +internal val OBJCNAMES_CLASSES_PACKAGE = CirPackageName.create(NativeForwardDeclarationKind.ObjCClass.packageFqName) -internal val OBJCNAMES_PROTOCOLS_PACKAGE = CirPackageName.create("objcnames.protocols") +internal val OBJCNAMES_PROTOCOLS_PACKAGE = CirPackageName.create(NativeForwardDeclarationKind.ObjCProtocol.packageFqName) -private val CINTEROP_PACKAGE: CirPackageName = CirPackageName.create("kotlinx.cinterop") +private val CINTEROP_PACKAGE: CirPackageName = CirPackageName.create(NativeStandardInteropNames.cInteropPackage) private val OBJC_INTEROP_CALLABLE_ANNOTATIONS: List = listOf( CirName.create("ObjCMethod"),