Native, K1: add ExperimentalForeignApi to generated forward declarations

cinterop tool should add ExperimentalForeignApi to all generated
declarations, to indicate their experimental status and discourage using
them in public Kotlin API. But the same considerations are applicable
to forward declarations (cnames.*, objcnames.*), which are generated not
by cinterop tool, but directly by the compiler.

This commit adds ExperimentalForeignApi to those compiler-generated
classes.

^KT-58362
This commit is contained in:
Svyatoslav Scherbina
2023-07-14 12:36:47 +02:00
committed by Space Team
parent d6ba233bbe
commit cba955cb3e
2 changed files with 20 additions and 4 deletions
@@ -8,6 +8,8 @@ package org.jetbrains.kotlin.library.metadata.impl
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
@@ -182,14 +184,26 @@ class ForwardDeclarationsPackageFragmentDescriptor(
private val declarations = storageManager.createMemoizedFunction(this::createDeclaration) private val declarations = storageManager.createMemoizedFunction(this::createDeclaration)
private val supertype by storageManager.createLazyValue { private val supertype by storageManager.createLazyValue {
val descriptor = builtIns.builtInsModule.getPackage(NativeStandardInteropNames.cInteropPackage) findCinteropClass(supertypeName).defaultType
.memberScope }
.getContributedClassifier(supertypeName, NoLookupLocation.FROM_BACKEND) as ClassDescriptor
descriptor.defaultType private val experimentalAnnotationType by storageManager.createLazyValue {
findCinteropClass(NativeStandardInteropNames.ExperimentalForeignApi).defaultType
}
private fun findCinteropClass(name: Name): ClassDescriptor {
return builtIns.builtInsModule.getPackage(NativeStandardInteropNames.cInteropPackage)
.memberScope
.getContributedClassifier(name, NoLookupLocation.FROM_BACKEND) as ClassDescriptor
} }
private fun createDeclaration(name: Name): ClassDescriptor { private fun createDeclaration(name: Name): ClassDescriptor {
val experimentalAnnotation = AnnotationDescriptorImpl(
experimentalAnnotationType,
emptyMap(),
SourceElement.NO_SOURCE
)
return object : ClassDescriptorImpl( return object : ClassDescriptorImpl(
this@ForwardDeclarationsPackageFragmentDescriptor, this@ForwardDeclarationsPackageFragmentDescriptor,
name, name,
@@ -201,6 +215,7 @@ class ForwardDeclarationsPackageFragmentDescriptor(
LockBasedStorageManager.NO_LOCKS LockBasedStorageManager.NO_LOCKS
) { ) {
override fun isExpect(): Boolean = isExpect override fun isExpect(): Boolean = isExpect
override val annotations: Annotations = Annotations.create(listOf(experimentalAnnotation))
}.apply { }.apply {
this.initialize(MemberScope.Empty, emptySet(), null) this.initialize(MemberScope.Empty, emptySet(), null)
} }
@@ -12,6 +12,7 @@ object NativeStandardInteropNames {
internal val CStructVar = Name.identifier("CStructVar") internal val CStructVar = Name.identifier("CStructVar")
internal val ObjCObjectBase = Name.identifier("ObjCObjectBase") internal val ObjCObjectBase = Name.identifier("ObjCObjectBase")
internal val ObjCObject = Name.identifier("ObjCObject") internal val ObjCObject = Name.identifier("ObjCObject")
val ExperimentalForeignApi = Name.identifier("ExperimentalForeignApi")
object ForwardDeclarations { object ForwardDeclarations {
private val cNamesPackage = FqName("cnames") private val cNamesPackage = FqName("cnames")