Promote DeprecationLevel on Visitors API to ERROR

Rewrite Readers.kt and Writers.kt to avoid using
Visitors API

Align KotlinCommonMetadata/KotlinModuleMetadata API with
KotlinClassMetadata one (it is necessary for restructured reading/writing)

#KT-59442 In Progress
This commit is contained in:
Leonid Startsev
2023-07-18 19:58:31 +02:00
committed by Space Team
parent 4e3dbcada3
commit b8e4b44b04
24 changed files with 827 additions and 1126 deletions
@@ -1584,11 +1584,8 @@ public final class kotlinx/metadata/jvm/KotlinModuleMetadata$Companion {
public static synthetic fun write$default (Lkotlinx/metadata/jvm/KotlinModuleMetadata$Companion;Lkotlinx/metadata/jvm/KmModule;[IILjava/lang/Object;)[B
}
public final class kotlinx/metadata/jvm/KotlinModuleMetadata$Writer : kotlinx/metadata/jvm/KmModuleVisitor {
public final class kotlinx/metadata/jvm/KotlinModuleMetadata$Writer {
public fun <init> ()V
public fun visitAnnotation (Lkotlinx/metadata/KmAnnotation;)V
public fun visitOptionalAnnotationClass ()Lkotlinx/metadata/KmClassVisitor;
public fun visitPackageParts (Ljava/lang/String;Ljava/util/List;Ljava/util/Map;)V
public final fun write ([I)[B
public static synthetic fun write$default (Lkotlinx/metadata/jvm/KotlinModuleMetadata$Writer;[IILjava/lang/Object;)[B
}
@@ -2,7 +2,7 @@
* Copyright 2010-2018 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.jvm
@@ -12,7 +12,7 @@ import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
/**
* A visitor containing the common code to visit JVM extensions for Kotlin declaration containers, such as classes and package fragments.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public abstract class JvmDeclarationContainerExtensionVisitor @JvmOverloads constructor(
protected open val delegate: JvmDeclarationContainerExtensionVisitor? = null
) : KmDeclarationContainerExtensionVisitor {
@@ -46,7 +46,7 @@ public abstract class JvmDeclarationContainerExtensionVisitor @JvmOverloads cons
/**
* A visitor to visit JVM extensions for a class.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmClassExtensionVisitor @JvmOverloads constructor(
delegate: JvmClassExtensionVisitor? = null
) : KmClassExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) {
@@ -92,7 +92,7 @@ public open class JvmClassExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a package fragment.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmPackageExtensionVisitor @JvmOverloads constructor(
delegate: JvmPackageExtensionVisitor? = null
) : KmPackageExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) {
@@ -123,7 +123,7 @@ public open class JvmPackageExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a function.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmFunctionExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmFunctionExtensionVisitor? = null
) : KmFunctionExtensionVisitor {
@@ -170,7 +170,7 @@ public open class JvmFunctionExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a property.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmPropertyExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmPropertyExtensionVisitor? = null
) : KmPropertyExtensionVisitor {
@@ -259,7 +259,7 @@ public open class JvmPropertyExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a constructor.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmConstructorExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmConstructorExtensionVisitor? = null
) : KmConstructorExtensionVisitor {
@@ -291,7 +291,7 @@ public open class JvmConstructorExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a type parameter.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmTypeParameterExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmTypeParameterExtensionVisitor? = null
) : KmTypeParameterExtensionVisitor {
@@ -328,7 +328,7 @@ public open class JvmTypeParameterExtensionVisitor @JvmOverloads constructor(
/**
* A visitor to visit JVM extensions for a type.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public open class JvmTypeExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmTypeExtensionVisitor? = null
) : KmTypeExtensionVisitor {
@@ -2,7 +2,7 @@
* 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR", "UNUSED_PARAMETER")
package kotlinx.metadata.jvm
@@ -58,9 +58,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* Returns the same (mutable) [KmClass] instance every time.
*/
public val kmClass: KmClass = KmClass().apply {
public val kmClass: KmClass = run {
val (strings, proto) = JvmProtoBufUtil.readClassDataFrom(annotationData.requireNotEmpty(), annotationData.data2)
proto.accept(this, strings)
proto.toKmClass(strings)
}
/**
@@ -78,7 +78,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* @param v the visitor that must visit this class
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmClassVisitor): Unit = kmClass.accept(v)
/**
@@ -86,6 +86,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public class Writer : ClassWriter(JvmStringTable()) {
/**
@@ -99,15 +100,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@JvmOverloads
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0
): Class {
checkMetadataVersion(metadataVersion)
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(CLASS_KIND, metadataVersion, d1, d2, extraInt = extraInt)
return Class(metadata)
error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeClass.")
}
}
}
@@ -127,9 +126,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* Returns the same (mutable) [KmPackage] instance every time.
*/
public val kmPackage: KmPackage = KmPackage().apply {
public val kmPackage: KmPackage = run {
val (strings, proto) = JvmProtoBufUtil.readPackageDataFrom(annotationData.requireNotEmpty(), annotationData.data2)
proto.accept(this, strings)
proto.toKmPackage(strings)
}
/**
@@ -147,7 +146,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* @param v the visitor that must visit this file facade
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmPackageVisitor): Unit = kmPackage.accept(v)
/**
@@ -155,6 +154,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public class Writer : PackageWriter(JvmStringTable()) {
/**
@@ -168,15 +168,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@JvmOverloads
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0
): FileFacade {
checkMetadataVersion(metadataVersion)
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(FILE_FACADE_KIND, metadataVersion, d1, d2, extraInt = extraInt)
return FileFacade(metadata)
error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeFileFacade.")
}
}
}
@@ -203,9 +201,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* Returns the same (mutable) [KmLambda] instance every time.
*/
public val kmLambda: KmLambda? = if (!isLambda) null else KmLambda().apply {
public val kmLambda: KmLambda? = if (!isLambda) null else {
val (strings, proto) = functionData!!
proto.accept(this, strings)
proto.toKmLambda(strings)
}
/**
@@ -227,7 +225,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* @param v the visitor that must visit this lambda
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmLambdaVisitor) {
if (!isLambda) throw IllegalArgumentException(
"accept(KmLambdaVisitor) is only possible for synthetic classes which are lambdas (isLambda = true)"
@@ -244,6 +242,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@Deprecated(
WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " +
"or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class",
level = DeprecationLevel.ERROR
)
public class Writer : LambdaWriter(JvmStringTable()) {
/**
@@ -257,19 +256,14 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@Deprecated(
WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " +
"or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class",
level = DeprecationLevel.ERROR
)
@JvmOverloads
public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0
): SyntheticClass {
checkMetadataVersion(metadataVersion)
val proto = t?.build()
val (d1, d2) =
if (proto != null) writeProtoBufData(proto, c)
else Pair(emptyArray(), emptyArray())
val metadata = Metadata(SYNTHETIC_CLASS_KIND, metadataVersion, d1, d2, extraInt = extraInt)
return SyntheticClass(metadata)
error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeLambda or KotlinClassMetadata.writeSyntheticClass.")
}
}
}
@@ -316,6 +310,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public class Writer {
/**
@@ -329,6 +324,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
@JvmOverloads
public fun write(
@@ -336,12 +332,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0
): MultiFileClassFacade {
checkMetadataVersion(metadataVersion)
val metadata = Metadata(
MULTI_FILE_CLASS_FACADE_KIND, metadataVersion,
partClassNames.toTypedArray(), extraInt = extraInt
)
return MultiFileClassFacade(metadata)
error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeMultiFileClassFacade.")
}
}
}
@@ -363,9 +354,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* Returns the same (mutable) [KmPackage] instance every time.
*/
public val kmPackage: KmPackage = KmPackage().apply {
public val kmPackage: KmPackage = run {
val (strings, proto) = JvmProtoBufUtil.readPackageDataFrom(annotationData.requireNotEmpty(), annotationData.data2)
proto.accept(this, strings)
proto.toKmPackage(strings)
}
/**
@@ -389,7 +380,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*
* @param v the visitor that must visit this multi-file class part
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmPackageVisitor) {
kmPackage.accept(v)
}
@@ -398,7 +389,8 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* A [KmPackageVisitor] that generates the metadata of a multi-file class part.
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassPart(kmPackage, facadeClassName, metadataVersion, extraInt)"
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassPart(kmPackage, facadeClassName, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
public class Writer : PackageWriter(JvmStringTable()) {
/**
@@ -411,7 +403,8 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* 0 by default
*/
@Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassPart(kmPackage, facadeClassName, metadataVersion, extraInt)"
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassPart(kmPackage, facadeClassName, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
)
@JvmOverloads
public fun write(
@@ -419,12 +412,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0
): MultiFileClassPart {
checkMetadataVersion(metadataVersion)
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(
MULTI_FILE_CLASS_PART_KIND, metadataVersion, d1, d2, facadeClassName, extraInt = extraInt
)
return MultiFileClassPart(metadata)
error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeMultifileClassPart.")
}
}
}
@@ -457,7 +445,11 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE {
Class.Writer().also { kmClass.accept(it) }.write(metadataVersion, extraInt).annotationData
checkMetadataVersion(metadataVersion)
val writer = ClassWriter(JvmStringTable())
writer.writeClass(kmClass)
val (d1, d2) = writeProtoBufData(writer.t.build(), writer.c)
Metadata(CLASS_KIND, metadataVersion, d1, d2, extraInt = extraInt)
}
/**
@@ -477,7 +469,11 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE {
FileFacade.Writer().also { kmPackage.accept(it) }.write(metadataVersion, extraInt).annotationData
checkMetadataVersion(metadataVersion)
val writer = PackageWriter(JvmStringTable())
writer.writePackage(kmPackage)
val (d1, d2) = writeProtoBufData(writer.t.build(), writer.c)
Metadata(FILE_FACADE_KIND, metadataVersion, d1, d2, extraInt = extraInt)
}
/**
@@ -497,7 +493,14 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE {
SyntheticClass.Writer().also { kmLambda.accept(it) }.write(metadataVersion, extraInt).annotationData
checkMetadataVersion(metadataVersion)
val writer = LambdaWriter(JvmStringTable())
writer.writeLambda(kmLambda)
val proto = writer.t?.build()
val (d1, d2) =
if (proto != null) writeProtoBufData(proto, writer.c)
else Pair(emptyArray(), emptyArray())
Metadata(SYNTHETIC_CLASS_KIND, metadataVersion, d1, d2, extraInt = extraInt)
}
/**
@@ -515,7 +518,10 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
public fun writeSyntheticClass(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = SyntheticClass.Writer().write(metadataVersion, extraInt).annotationData
): Metadata {
checkMetadataVersion(metadataVersion)
return Metadata(SYNTHETIC_CLASS_KIND, metadataVersion, emptyArray(), emptyArray(), extraInt = extraInt)
}
/**
* Writes metadata of the multi-file class facade.
@@ -533,7 +539,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
public fun writeMultiFileClassFacade(
partClassNames: List<String>, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = MultiFileClassFacade.Writer().write(partClassNames, metadataVersion, extraInt).annotationData
): Metadata {
checkMetadataVersion(metadataVersion)
return Metadata(
MULTI_FILE_CLASS_FACADE_KIND, metadataVersion,
partClassNames.toTypedArray(), extraInt = extraInt
)
}
/**
* Writes the metadata of the multi-file class part.
@@ -554,7 +566,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE {
MultiFileClassPart.Writer().also { kmPackage.accept(it) }.write(facadeClassName, metadataVersion, extraInt).annotationData
checkMetadataVersion(metadataVersion)
val writer = PackageWriter(JvmStringTable())
writer.writePackage(kmPackage)
val (d1, d2) = writeProtoBufData(writer.t.build(), writer.c)
Metadata(
MULTI_FILE_CLASS_PART_KIND, metadataVersion, d1, d2, facadeClassName, extraInt = extraInt
)
}
/**
@@ -6,15 +6,16 @@
@file:Suppress(
"MemberVisibilityCanBePrivate",
"DEPRECATION",
"DEPRECATION_ERROR",
"INVISIBLE_MEMBER", // InconsistentKotlinMetadataException
"INVISIBLE_REFERENCE"
"INVISIBLE_REFERENCE",
"UNUSED_PARAMETER"
)
package kotlinx.metadata.jvm
import kotlinx.metadata.*
import kotlinx.metadata.internal.accept
import kotlinx.metadata.jvm.internal.IgnoreInApiDump
import kotlinx.metadata.internal.toKmClass
import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.COMPATIBLE_METADATA_VERSION
import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.throwIfNotCompatible
import kotlinx.metadata.jvm.internal.wrapIntoMetadataExceptionWhenNeeded
@@ -47,7 +48,7 @@ public class KotlinModuleMetadata private constructor(
*
* Returns the same (mutable) [KmModule] instance every time.
*/
public val kmModule: KmModule = KmModule().apply(this::accept)
public val kmModule: KmModule = readImpl()
/**
* Visits metadata of this module with a new [KmModule] instance and returns that instance.
@@ -57,39 +58,42 @@ public class KotlinModuleMetadata private constructor(
ReplaceWith("kmModule"),
DeprecationLevel.WARNING
)
public fun toKmModule(): KmModule = KmModule().apply(kmModule::accept)
public fun toKmModule(): KmModule = KmModule().apply { kmModule.accept(this) }
/**
* A [KmModuleVisitor] that generates the metadata of a Kotlin JVM module file.
*/
@Deprecated("Writer API is deprecated as excessive and cumbersome. Please use KotlinModuleMetadata.write(kmModule, metadataVersion)")
public class Writer : KmModuleVisitor() {
@Deprecated(
"Writer API is deprecated as excessive and cumbersome. Please use KotlinModuleMetadata.write(kmModule, metadataVersion)",
level = DeprecationLevel.ERROR
)
public class Writer {
private val b = JvmModuleProtoBuf.Module.newBuilder()
override fun visitPackageParts(fqName: String, fileFacades: List<String>, multiFileClassParts: Map<String, String>) {
PackageParts(fqName).apply {
for (fileFacade in fileFacades) {
addPart(fileFacade, null)
}
for ((multiFileClassPart, multiFileFacade) in multiFileClassParts) {
addPart(multiFileClassPart, multiFileFacade)
}
private fun writeModule(kmModule: KmModule) {
kmModule.packageParts.forEach { (fqName, packageParts) ->
PackageParts(fqName).apply {
for (fileFacade in packageParts.fileFacades) {
addPart(fileFacade, null)
}
for ((multiFileClassPart, multiFileFacade) in packageParts.multiFileClassParts) {
addPart(multiFileClassPart, multiFileFacade)
}
addTo(b)
addTo(b)
}
}
}
override fun visitAnnotation(annotation: KmAnnotation) {
// visitAnnotation
/*
// TODO: move StringTableImpl to module 'metadata' and support module annotations here
b.addAnnotation(ProtoBuf.Annotation.newBuilder().apply {
id = annotation.className.name // <-- use StringTableImpl here
})
*/
}
override fun visitOptionalAnnotationClass(): KmClassVisitor? {
// visitOptionalAnnotationClass
/*
return object : ClassWriter(TODO() /* use StringTableImpl here */) {
override fun visitEnd() {
@@ -97,7 +101,6 @@ public class KotlinModuleMetadata private constructor(
}
}
*/
return null
}
/**
@@ -106,9 +109,12 @@ public class KotlinModuleMetadata private constructor(
* @param metadataVersion metadata version to be written to the metadata (see [Metadata.metadataVersion]),
* [KotlinClassMetadata.COMPATIBLE_METADATA_VERSION] by default
*/
@Deprecated("Writer API is deprecated as excessive and cumbersome. Please use KotlinModuleMetadata.write(kmModule, metadataVersion)")
@Deprecated(
"Writer API is deprecated as excessive and cumbersome. Please use KotlinModuleMetadata.write(kmModule, metadataVersion)",
level = DeprecationLevel.ERROR
)
public fun write(metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION): ByteArray {
return b.build().serializeToByteArray(JvmMetadataVersion(*metadataVersion), 0)
error("This method is no longer implemented. Migrate to KotlinModuleMetadata.write.")
}
}
@@ -117,24 +123,27 @@ public class KotlinModuleMetadata private constructor(
*
* @param v the visitor that must visit this module file
*/
@Deprecated(VISITOR_API_MESSAGE)
public fun accept(v: KmModuleVisitor) {
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmModuleVisitor): Unit = kmModule.accept(v)
private fun readImpl(): KmModule {
val v = KmModule()
for ((fqName, parts) in data.packageFqName2Parts) {
val (fileFacades, multiFileClassParts) = parts.parts.partition { parts.getMultifileFacadeName(it) == null }
v.visitPackageParts(fqName, fileFacades, multiFileClassParts.associateWith { parts.getMultifileFacadeName(it)!! })
v.packageParts[fqName] = KmPackageParts(
fileFacades.toMutableList(),
multiFileClassParts.associateWith { parts.getMultifileFacadeName(it)!! }.toMutableMap()
)
}
for (annotation in data.moduleData.annotations) {
v.visitAnnotation(KmAnnotation(annotation, emptyMap()))
v.annotations.add(KmAnnotation(annotation, emptyMap()))
}
for (classProto in data.moduleData.optionalAnnotations) {
v.visitOptionalAnnotationClass()?.let {
classProto.accept(it, data.moduleData.nameResolver)
}
v.optionalAnnotationClasses.add(classProto.toKmClass(data.moduleData.nameResolver))
}
v.visitEnd()
return v
}
/**
@@ -173,7 +182,8 @@ public class KotlinModuleMetadata private constructor(
@JvmStatic
@JvmOverloads
public fun write(kmModule: KmModule, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION): ByteArray = wrapWriteIntoIAE {
Writer().also { kmModule.accept(it) }.write(metadataVersion)
val w = Writer().also { it.writeModule(kmModule) }
return w.b.build().serializeToByteArray(JvmMetadataVersion(*metadataVersion), 0)
}
private fun dataFromBytes(bytes: ByteArray): ModuleMapping {
@@ -190,7 +200,7 @@ public class KotlinModuleMetadata private constructor(
*
* When using this class, [visitEnd] must be called exactly once and after calls to all other visit* methods.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@UnstableMetadataApi
public abstract class KmModuleVisitor(private val delegate: KmModuleVisitor? = null) {
/**
@@ -265,17 +275,17 @@ public class KmModule : KmModuleVisitor() {
*/
public val optionalAnnotationClasses: MutableList<KmClass> = ArrayList(0)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitPackageParts(fqName: String, fileFacades: List<String>, multiFileClassParts: Map<String, String>) {
packageParts[fqName] = KmPackageParts(fileFacades.toMutableList(), multiFileClassParts.toMutableMap())
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAnnotation(annotation: KmAnnotation) {
annotations.add(annotation)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOptionalAnnotationClass(): KmClass =
KmClass().also(optionalAnnotationClasses::add)
@@ -284,7 +294,7 @@ public class KmModule : KmModuleVisitor() {
*
* @param visitor the visitor which will visit data in this module.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmModuleVisitor) {
for ((fqName, parts) in packageParts) {
visitor.visitPackageParts(fqName, parts.fileFacades, parts.multiFileClassParts)
@@ -2,7 +2,7 @@
* Copyright 2010-2022 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION","DEPRECATION_ERROR")
package kotlinx.metadata.jvm.internal
@@ -15,10 +15,11 @@ import org.jetbrains.kotlin.metadata.deserialization.getExtensionOrNull
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
@Suppress("DEPRECATION")
@Suppress("DEPRECATION_ERROR")
internal class JvmMetadataExtensions : MetadataExtensions {
override fun readClassExtensions(v: KmClassVisitor, proto: ProtoBuf.Class, c: ReadContext) {
val ext = v.visitExtensions(JvmClassExtensionVisitor.TYPE) as? JvmClassExtensionVisitor ?: return
ext as JvmClassExtension
val anonymousObjectOriginName = proto.getExtensionOrNull(JvmProtoBuf.anonymousObjectOriginName)
if (anonymousObjectOriginName != null) {
@@ -26,9 +27,7 @@ internal class JvmMetadataExtensions : MetadataExtensions {
}
for (property in proto.getExtension(JvmProtoBuf.classLocalVariable)) {
ext.visitLocalDelegatedProperty(
property.flags, c[property.name], property.getPropertyGetterFlags(), property.getPropertySetterFlags()
)?.let { property.accept(it, c) }
ext.localDelegatedProperties.add(property.toKmProperty(c))
}
ext.visitModuleName(proto.getExtensionOrNull(JvmProtoBuf.classModuleName)?.let(c::get) ?: JvmProtoBufUtil.DEFAULT_MODULE_NAME)
@@ -40,11 +39,10 @@ internal class JvmMetadataExtensions : MetadataExtensions {
override fun readPackageExtensions(v: KmPackageVisitor, proto: ProtoBuf.Package, c: ReadContext) {
val ext = v.visitExtensions(JvmPackageExtensionVisitor.TYPE) as? JvmPackageExtensionVisitor ?: return
ext as JvmPackageExtension
for (property in proto.getExtension(JvmProtoBuf.packageLocalVariable)) {
ext.visitLocalDelegatedProperty(
property.flags, c[property.name], property.getPropertyGetterFlags(), property.getPropertySetterFlags()
)?.let { property.accept(it, c) }
ext.localDelegatedProperties.add(property.toKmProperty(c))
}
ext.visitModuleName(proto.getExtensionOrNull(JvmProtoBuf.packageModuleName)?.let(c::get) ?: JvmProtoBufUtil.DEFAULT_MODULE_NAME)
@@ -119,50 +117,34 @@ internal class JvmMetadataExtensions : MetadataExtensions {
override fun readValueParameterExtensions(v: KmValueParameterVisitor, proto: ProtoBuf.ValueParameter, c: ReadContext) {}
override fun writeClassExtensions(type: KmExtensionType, proto: ProtoBuf.Class.Builder, c: WriteContext): KmClassExtensionVisitor? {
if (type != JvmClassExtensionVisitor.TYPE) return null
return object : JvmClassExtensionVisitor() {
override fun visitAnonymousObjectOriginName(internalName: String) {
proto.setExtension(JvmProtoBuf.anonymousObjectOriginName, c[internalName])
}
override fun visitLocalDelegatedProperty(
flags: Int, name: String, getterFlags: Int, setterFlags: Int
): KmPropertyVisitor = writeProperty(c, flags, name, getterFlags, setterFlags) {
proto.addExtension(JvmProtoBuf.classLocalVariable, it.build())
}
override fun visitModuleName(name: String) {
if (name != JvmProtoBufUtil.DEFAULT_MODULE_NAME) {
proto.setExtension(JvmProtoBuf.classModuleName, c[name])
}
}
override fun visitJvmFlags(flags: Int) {
if (flags != 0) {
proto.setExtension(JvmProtoBuf.jvmClassFlags, flags)
}
}
override fun writeClassExtensions(extension: KmClassExtension, proto: ProtoBuf.Class.Builder, c: WriteContext): Boolean {
if (extension.type != JvmClassExtensionVisitor.TYPE) return false
extension as JvmClassExtension
extension.anonymousObjectOriginName?.let { proto.setExtension(JvmProtoBuf.anonymousObjectOriginName, c[it]) }
extension.localDelegatedProperties.forEach {
proto.addExtension(JvmProtoBuf.classLocalVariable, c.writeProperty(it).build())
}
extension.moduleName?.let { moduleName ->
if (moduleName != JvmProtoBufUtil.DEFAULT_MODULE_NAME) proto.setExtension(JvmProtoBuf.classModuleName, c[moduleName])
}
if (extension.jvmFlags != 0) proto.setExtension(JvmProtoBuf.jvmClassFlags, extension.jvmFlags)
return true
}
override fun writePackageExtensions(
type: KmExtensionType, proto: ProtoBuf.Package.Builder, c: WriteContext
): KmPackageExtensionVisitor? {
if (type != JvmPackageExtensionVisitor.TYPE) return null
return object : JvmPackageExtensionVisitor() {
override fun visitLocalDelegatedProperty(
flags: Int, name: String, getterFlags: Int, setterFlags: Int
): KmPropertyVisitor = writeProperty(c, flags, name, getterFlags, setterFlags) {
proto.addExtension(JvmProtoBuf.packageLocalVariable, it.build())
}
override fun visitModuleName(name: String) {
if (name != JvmProtoBufUtil.DEFAULT_MODULE_NAME) {
proto.setExtension(JvmProtoBuf.packageModuleName, c[name])
}
extension: KmPackageExtension, proto: ProtoBuf.Package.Builder, c: WriteContext
): Boolean {
if (extension.type != JvmPackageExtensionVisitor.TYPE) return false
extension as JvmPackageExtension
extension.localDelegatedProperties.forEach {
proto.addExtension(JvmProtoBuf.packageLocalVariable, c.writeProperty(it).build())
}
extension.moduleName?.let { name ->
if (name != JvmProtoBufUtil.DEFAULT_MODULE_NAME) {
proto.setExtension(JvmProtoBuf.packageModuleName, c[name])
}
}
return true
}
// PackageFragment is not used by JVM backend.
@@ -124,27 +124,15 @@ class MetadataSmokeTest {
@Test
fun jvmInternalName() {
class ClassNameReader : KmClassVisitor() {
lateinit var className: ClassName
override fun visit(flags: Int, name: ClassName) {
className = name
}
}
class L
val l = ClassNameReader().run {
(KotlinClassMetadata.read(L::class.java.getMetadata()) as KotlinClassMetadata.Class).accept(this)
className
}
val l = (KotlinClassMetadata.read(L::class.java.getMetadata()) as KotlinClassMetadata.Class).kmClass.name
assertEquals(".kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l)
assertEquals("kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l.toJvmInternalName())
val coroutineContextKey = ClassNameReader().run {
(KotlinClassMetadata.read(CoroutineContext.Key::class.java.getMetadata()) as KotlinClassMetadata.Class).accept(this)
className
}
val coroutineContextKey = (KotlinClassMetadata.read(CoroutineContext.Key::class.java.getMetadata()) as KotlinClassMetadata.Class).kmClass.name
assertEquals("kotlin/coroutines/CoroutineContext.Key", coroutineContextKey)
assertEquals("kotlin/coroutines/CoroutineContext\$Key", coroutineContextKey.toJvmInternalName())
}
@@ -154,7 +142,7 @@ class MetadataSmokeTest {
val x: suspend Int.(String, String) -> Unit = { _, _ -> }
val annotation = x::class.java.getMetadata()
val metadata = KotlinClassMetadata.read(annotation) as KotlinClassMetadata.SyntheticClass
metadata.accept(KmLambda())
assertNotNull(metadata.kmLambda)
}
@Test
@@ -172,19 +160,10 @@ class MetadataSmokeTest {
classWithStableParameterNames.constructors.forEach { assertFalse(it.hasNonStableParameterNames) }
classWithStableParameterNames.functions.forEach { assertFalse(it.hasNonStableParameterNames) }
val newMetadata = KotlinClassMetadata.writeClass(
KmClass().apply {
classWithStableParameterNames.accept(
object : KmClassVisitor(this) {
override fun visitConstructor(flags: Int) =
super.visitConstructor(flags + flagsOf(Flag.Constructor.HAS_NON_STABLE_PARAMETER_NAMES))
classWithStableParameterNames.constructors.forEach { it.hasNonStableParameterNames = true }
classWithStableParameterNames.functions.forEach { it.hasNonStableParameterNames = true }
override fun visitFunction(flags: Int, name: String) =
super.visitFunction(flags + flagsOf(Flag.Function.HAS_NON_STABLE_PARAMETER_NAMES), name)
}
)
}
)
val newMetadata = KotlinClassMetadata.writeClass(classWithStableParameterNames)
val classWithUnstableParameterNames = newMetadata.readAsKmClass()
@@ -2,7 +2,7 @@
* 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.klib
@@ -90,7 +90,7 @@ class KlibModuleMetadata(
library.packageMetadataParts(packageFqName).map { part ->
val packageFragment = parsePackageFragment(library.packageMetadata(packageFqName, part))
val nameResolver = NameResolverImpl(packageFragment.strings, packageFragment.qualifiedNames)
KmModuleFragment().apply { packageFragment.accept(this, nameResolver, listOf(fileIndex)) }
packageFragment.toKmModuleFragment(nameResolver, listOf(fileIndex))
}.let(readStrategy::processModuleParts)
}
return KlibModuleMetadata(moduleHeader.moduleName, moduleFragments, moduleHeader.annotation)
@@ -119,9 +119,9 @@ class KlibModuleMetadata(
annotations
)
val groupedProtos = groupedFragments.mapValues { (_, fragments) ->
fragments.map {
fragments.map { mf ->
val c = WriteContext(ApproximatingStringTable(), listOf(reverseIndex))
KlibModuleFragmentWriter(c.strings as ApproximatingStringTable, c.contextExtensions).also(it::accept).write()
KlibModuleFragmentWriter(c.strings as ApproximatingStringTable, c.contextExtensions).also { it.writeModuleFragment(mf) }.write()
}
}
// This context and string table is only required for module-level annotations.
@@ -2,7 +2,7 @@
* 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.klib.impl
@@ -151,9 +151,9 @@ internal class KlibMetadataExtensions : MetadataExtensions {
}
}
override fun writeClassExtensions(type: KmExtensionType, proto: ProtoBuf.Class.Builder, c: WriteContext): KmClassExtensionVisitor? {
if (type != KlibClassExtensionVisitor.TYPE) return null
return object : KlibClassExtensionVisitor() {
override fun writeClassExtensions(extension: KmClassExtension, proto: ProtoBuf.Class.Builder, c: WriteContext): Boolean {
if (extension.type != KlibClassExtensionVisitor.TYPE) return false
extension.accept(object : KlibClassExtensionVisitor() {
override fun visitAnnotation(annotation: KmAnnotation) {
proto.addExtension(
KlibMetadataProtoBuf.classAnnotation,
@@ -191,21 +191,23 @@ internal class KlibMetadataExtensions : MetadataExtensions {
proto.setEnumEntry(entryIndex, entryProto.build())
}
}
}
})
return true
}
override fun writePackageExtensions(
type: KmExtensionType,
extension: KmPackageExtension,
proto: ProtoBuf.Package.Builder,
c: WriteContext
): KmPackageExtensionVisitor? {
if (type != KlibPackageExtensionVisitor.TYPE) return null
return object : KlibPackageExtensionVisitor() {
): Boolean {
if (extension.type != KlibPackageExtensionVisitor.TYPE) return false
extension.accept(object : KlibPackageExtensionVisitor() {
override fun visitFqName(name: String) {
val nameIdx = (c.strings as StringTableImpl).getPackageFqNameIndex(FqName(name))
proto.setExtension(KlibMetadataProtoBuf.packageFqName, nameIdx)
}
}
})
return true
}
override fun writeModuleFragmentExtensions(
@@ -2,11 +2,11 @@
* 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.
*/
@file:Suppress("DEPRECATION")
package kotlinx.metadata.klib.impl
import kotlinx.metadata.internal.*
import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.klib.KlibSourceFile
import org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf
import org.jetbrains.kotlin.metadata.ProtoBuf
@@ -34,7 +34,8 @@ class KlibModuleFragmentWriter(
fun write(): ProtoBuf.PackageFragment =
t.build()
override fun visitEnd() {
override fun writeModuleFragment(kmPackageFragment: KmModuleFragment) {
super.writeModuleFragment(kmPackageFragment)
// TODO: This should be moved to ModuleFragmentWriter.
val (strings, qualifiedNames) = (c.strings as ApproximatingStringTable).buildProto()
@@ -49,4 +50,4 @@ class KlibModuleFragmentWriter(
val isEmpty = t.class_Count == 0 && isPackageEmpty
t.setExtension(KlibMetadataProtoBuf.isEmpty, isEmpty)
}
}
}
@@ -2,7 +2,7 @@
* 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.klib
@@ -2,7 +2,7 @@
* Copyright 2010-2018 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata
@@ -29,7 +29,7 @@ import kotlin.reflect.KClass
* ```
* In case an extension visitor of an unrelated type is returned, the code using the visitor API must ignore that visitor.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public class KmExtensionType(private val klass: KClass<out KmExtensionVisitor>) {
override fun equals(other: Any?): Boolean =
other is KmExtensionType && klass == other.klass
@@ -44,7 +44,7 @@ public class KmExtensionType(private val klass: KClass<out KmExtensionVisitor>)
/**
* A base interface for all extension visitors.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmExtensionVisitor {
/**
* Type of this extension visitor.
@@ -55,59 +55,59 @@ public interface KmExtensionVisitor {
/**
* A visitor to visit platform-specific extensions for a declaration container, such as a class or a package fragment.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmDeclarationContainerExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a class.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmClassExtensionVisitor : KmDeclarationContainerExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a package fragment.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmPackageExtensionVisitor : KmDeclarationContainerExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a function.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmFunctionExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a property.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmPropertyExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a constructor.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmConstructorExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a type parameter.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmTypeParameterExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a type.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmTypeExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a type alias.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmTypeAliasExtensionVisitor : KmExtensionVisitor
/**
* A visitor to visit platform-specific extensions for a value parameter.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmValueParameterExtensionVisitor : KmExtensionVisitor
@@ -38,7 +38,7 @@ public interface KmDeclarationContainer {
* Precise kind of the class can be obtained via [KmClass.kind].
* Various class attributes can be read and manipulated via extension properties, such as [KmClass.visibility] or [KmClass.isData].
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmClass : KmClassVisitor(), KmDeclarationContainer {
/**
* Class flags, consisting of [Flag.HAS_ANNOTATIONS], visibility flag, modality flag and [Flag.Class] flags.
@@ -122,78 +122,78 @@ public class KmClass : KmClassVisitor(), KmDeclarationContainer {
*/
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmClassExtension> =
internal val extensions: List<KmClassExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createClassExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visit(flags: Int, name: ClassName) {
this.flags = flags
this.name = name
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSupertype(flags: Int): KmTypeVisitor =
KmType(flags).addTo(supertypes)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).addTo(functions)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor =
KmProperty(flags, name, getterFlags, setterFlags).addTo(properties)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor =
KmTypeAlias(flags, name).addTo(typeAliases)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstructor(flags: Int): KmConstructorVisitor =
KmConstructor(flags).addTo(constructors)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitCompanionObject(name: String) {
this.companionObject = name
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitNestedClass(name: String) {
nestedClasses.add(name)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitEnumEntry(name: String) {
enumEntries.add(name)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSealedSubclass(name: ClassName) {
sealedSubclasses.add(name)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitInlineClassUnderlyingPropertyName(name: String) {
inlineClassUnderlyingPropertyName = name
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitInlineClassUnderlyingType(flags: Int): KmTypeVisitor =
KmType(flags).also { inlineClassUnderlyingType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmClassExtensionVisitor =
extensions.singleOfType(type)
@@ -202,7 +202,7 @@ public class KmClass : KmClassVisitor(), KmDeclarationContainer {
*
* @param visitor the visitor which will visit data in this class
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@OptIn(ExperimentalContextReceivers::class)
public fun accept(visitor: KmClassVisitor) {
visitor.visit(flags, name)
@@ -230,7 +230,7 @@ public class KmClass : KmClassVisitor(), KmDeclarationContainer {
* Package fragments are produced from single file facades and multi-file class parts.
* Note that a package fragment does not contain any classes, as classes are not a part of file facades and have their own metadata.
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
/**
* Functions in the package fragment.
@@ -247,22 +247,22 @@ public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
*/
override val typeAliases: MutableList<KmTypeAlias> = ArrayList(0)
private val extensions: List<KmPackageExtension> =
internal val extensions: List<KmPackageExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createPackageExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).addTo(functions)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor =
KmProperty(flags, name, getterFlags, setterFlags).addTo(properties)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor =
KmTypeAlias(flags, name).addTo(typeAliases)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmPackageExtensionVisitor =
extensions.singleOfType(type)
@@ -271,7 +271,7 @@ public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
*
* @param visitor the visitor which will visit data in this package fragment
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmPackageVisitor) {
functions.forEach { visitor.visitFunction(it.flags, it.name)?.let(it::accept) }
properties.forEach { visitor.visitProperty(it.flags, it.name, it.getter.flags, it.setterFlags)?.let(it::accept) }
@@ -284,14 +284,14 @@ public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
/**
* Represents a synthetic class generated for a Kotlin lambda.
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmLambda : KmLambdaVisitor() {
/**
* Signature of the synthetic anonymous function, representing the lambda.
*/
public lateinit var function: KmFunction
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).also { function = it }
@@ -300,7 +300,7 @@ public class KmLambda : KmLambdaVisitor() {
*
* @param visitor the visitor which will visit data in this lambda
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmLambdaVisitor) {
visitor.visitFunction(function.flags, function.name)?.let(function::accept)
visitor.visitEnd()
@@ -313,7 +313,7 @@ public class KmLambda : KmLambdaVisitor() {
* Various constructor attributes can be read and manipulated via extension properties,
* such as [KmConstructor.visibility] or [KmConstructor.isSecondary].
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmConstructor, such as KmConstructor.visibility") public var flags: Int,
) :
@@ -330,18 +330,18 @@ public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
*/
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmConstructorExtension> =
internal val extensions: List<KmConstructorExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createConstructorExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).addTo(valueParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmConstructorExtensionVisitor =
extensions.singleOfType(type)
@@ -350,7 +350,7 @@ public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
*
* @param visitor the visitor which will visit data in this class
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmConstructorVisitor) {
valueParameters.forEach { visitor.visitValueParameter(it.flags, it.name)?.let(it::accept) }
versionRequirements.forEach { visitor.visitVersionRequirement()?.let(it::accept) }
@@ -367,7 +367,7 @@ public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
*
* @property name the name of the function
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmFunction, such as KmFunction.visibility") public var flags: Int,
public var name: String,
@@ -412,40 +412,40 @@ public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
@ExperimentalContracts
public var contract: KmContract? = null
private val extensions: List<KmFunctionExtension> =
internal val extensions: List<KmFunctionExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createFunctionExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor =
KmType(flags).also { receiverParameterType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).addTo(valueParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReturnType(flags: Int): KmTypeVisitor =
KmType(flags).also { returnType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContracts
override fun visitContract(): KmContractVisitor =
KmContract().also { contract = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmFunctionExtensionVisitor =
extensions.singleOfType(type)
@@ -455,7 +455,7 @@ public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this function
*/
@OptIn(ExperimentalContextReceivers::class)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmFunctionVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
receiverParameterType?.let { visitor.visitReceiverParameterType(it.flags)?.let(it::accept) }
@@ -489,7 +489,7 @@ public class KmPropertyAccessorAttributes internal constructor(internal var flag
*
* @property name the name of the property
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmProperty, such as KmProperty.visibility") public var flags: Int,
public var name: String,
@@ -593,35 +593,35 @@ public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
*/
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmPropertyExtension> =
internal val extensions: List<KmPropertyExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createPropertyExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor =
KmType(flags).also { receiverParameterType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSetterParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).also { setterParameter = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReturnType(flags: Int): KmTypeVisitor =
KmType(flags).also { returnType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmPropertyExtensionVisitor =
extensions.singleOfType(type)
@@ -631,7 +631,7 @@ public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this property
*/
@OptIn(ExperimentalContextReceivers::class)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmPropertyVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
receiverParameterType?.let { visitor.visitReceiverParameterType(it.flags)?.let(it::accept) }
@@ -652,7 +652,7 @@ public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
*
* @property name the name of the type alias
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmTypeAlias, such as KmTypeAlias.visibility") public var flags: Int,
public var name: String,
@@ -686,31 +686,31 @@ public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
*/
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmTypeAliasExtension> =
internal val extensions: List<KmTypeAliasExtension> =
MetadataExtensions.INSTANCES.mapNotNull(MetadataExtensions::createTypeAliasExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitUnderlyingType(flags: Int): KmTypeVisitor =
KmType(flags).also { underlyingType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExpandedType(flags: Int): KmTypeVisitor =
KmType(flags).also { expandedType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAnnotation(annotation: KmAnnotation) {
annotations.add(annotation)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeAliasExtensionVisitor? =
extensions.singleOfType(type)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements)
@@ -719,7 +719,7 @@ public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
*
* @param visitor the visitor which will visit data in this type alias
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmTypeAliasVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
visitor.visitUnderlyingType(underlyingType.flags)?.let(underlyingType::accept)
@@ -739,7 +739,7 @@ public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
*
* @property name the name of the value parameter
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmValueParameter, such as KmValueParameter.declaresDefaultValue") public var flags: Int,
public var name: String,
@@ -758,18 +758,18 @@ public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
*/
public var varargElementType: KmType? = null
private val extensions: List<KmValueParameterExtension> =
internal val extensions: List<KmValueParameterExtension> =
MetadataExtensions.INSTANCES.mapNotNull(MetadataExtensions::createValueParameterExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitType(flags: Int): KmTypeVisitor =
KmType(flags).also { type = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVarargElementType(flags: Int): KmTypeVisitor =
KmType(flags).also { varargElementType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmValueParameterExtensionVisitor? =
extensions.singleOfType(type)
@@ -778,7 +778,7 @@ public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
*
* @param visitor the visitor which will visit data in this value parameter
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmValueParameterVisitor) {
visitor.visitType(type.flags)?.let(type::accept)
varargElementType?.let { visitor.visitVarargElementType(it.flags)?.let(it::accept) }
@@ -798,7 +798,7 @@ public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
* the name is not enough (e.g. `class A<T> { fun <T> foo(t: T) }`)
* @property variance the declaration-site variance of the type parameter
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmTypeParameter, such as KmTypeParameter.isReified") public var flags: Int,
public var name: String,
@@ -813,14 +813,14 @@ public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
*/
public val upperBounds: MutableList<KmType> = ArrayList(1)
private val extensions: List<KmTypeParameterExtension> =
internal val extensions: List<KmTypeParameterExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeParameterExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitUpperBound(flags: Int): KmTypeVisitor =
KmType(flags).addTo(upperBounds)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeParameterExtensionVisitor? =
extensions.singleOfType(type)
@@ -829,7 +829,7 @@ public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
*
* @param visitor the visitor which will visit data in this type parameter
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmTypeParameterVisitor) {
upperBounds.forEach { visitor.visitUpperBound(it.flags)?.let(it::accept) }
extensions.forEach { visitor.visitExtensions(it.type)?.let(it::accept) }
@@ -843,7 +843,7 @@ public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
* Various type attributes can be read and manipulated via extension properties,
* such as [KmType.isNullable].
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmType @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmType, such as KmType.isNullable") public var flags: Int
) : KmTypeVisitor() {
@@ -890,46 +890,46 @@ public class KmType @Deprecated(flagsCtorDeprecated) constructor(
*/
public var flexibleTypeUpperBound: KmFlexibleTypeUpperBound? = null
private val extensions: List<KmTypeExtension> =
internal val extensions: List<KmTypeExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeExtension)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitClass(name: ClassName) {
classifier = KmClassifier.Class(name)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeAlias(name: ClassName) {
classifier = KmClassifier.TypeAlias(name)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(id: Int) {
classifier = KmClassifier.TypeParameter(id)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitArgument(flags: Int, variance: KmVariance): KmTypeVisitor =
KmType(flags).also { arguments.add(KmTypeProjection(variance, it)) }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitStarProjection() {
arguments.add(KmTypeProjection.STAR)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAbbreviatedType(flags: Int): KmTypeVisitor =
KmType(flags).also { abbreviatedType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOuterType(flags: Int): KmTypeVisitor =
KmType(flags).also { outerType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFlexibleTypeUpperBound(flags: Int, typeFlexibilityId: String?): KmTypeVisitor =
KmType(flags).also { flexibleTypeUpperBound = KmFlexibleTypeUpperBound(it, typeFlexibilityId) }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeExtensionVisitor =
extensions.singleOfType(type)
@@ -939,7 +939,7 @@ public class KmType @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this type
* @throws IllegalArgumentException if type metadata is inconsistent
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmTypeVisitor) {
when (val classifier = classifier) {
is KmClassifier.Class -> visitor.visitClass(classifier.name)
@@ -969,7 +969,7 @@ public class KmType @Deprecated(flagsCtorDeprecated) constructor(
* Version requirement is an internal feature of the Kotlin compiler and the standard Kotlin library,
* enabled, for example, with the internal [kotlin.internal.RequireKotlin] annotation.
*/
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmVersionRequirement : KmVersionRequirementVisitor() {
/**
* Kind of the version that this declaration requires.
@@ -996,7 +996,7 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
*/
public lateinit var version: KmVersion
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visit(kind: KmVersionRequirementVersionKind, level: KmVersionRequirementLevel, errorCode: Int?, message: String?) {
this.kind = kind
this.level = level
@@ -1004,7 +1004,7 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
this.message = message
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersion(major: Int, minor: Int, patch: Int) {
this.version = KmVersion(major, minor, patch)
}
@@ -1014,7 +1014,7 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
*
* @param visitor the visitor which will visit data in this version requirement
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmVersionRequirementVisitor) {
visitor.visit(kind, level, errorCode, message)
visitor.visitVersion(version.major, version.minor, version.patch)
@@ -1029,14 +1029,14 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
* may change in a subsequent release.
*/
@ExperimentalContracts
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmContract : KmContractVisitor() {
/**
* Effects of this contract.
*/
public val effects: MutableList<KmEffect> = ArrayList(1)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitEffect(type: KmEffectType, invocationKind: KmEffectInvocationKind?): KmEffectVisitor =
KmEffect(type, invocationKind).addTo(effects)
@@ -1045,7 +1045,7 @@ public class KmContract : KmContractVisitor() {
*
* @param visitor the visitor which will visit data in this contract
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmContractVisitor) {
effects.forEach { visitor.visitEffect(it.type, it.invocationKind)?.let(it::accept) }
visitor.visitEnd()
@@ -1063,7 +1063,7 @@ public class KmContract : KmContractVisitor() {
* specified further in the effect expression
*/
@ExperimentalContracts
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmEffect(
public var type: KmEffectType,
public var invocationKind: KmEffectInvocationKind?
@@ -1079,11 +1079,11 @@ public class KmEffect(
*/
public var conclusion: KmEffectExpression? = null
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstructorArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(constructorArguments)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConclusionOfConditionalEffect(): KmEffectExpressionVisitor =
KmEffectExpression().also { conclusion = it }
@@ -1092,7 +1092,7 @@ public class KmEffect(
*
* @param visitor the visitor which will visit data in this effect
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmEffectVisitor) {
constructorArguments.forEach { visitor.visitConstructorArgument()?.let(it::accept) }
conclusion?.let { visitor.visitConclusionOfConditionalEffect()?.let(it::accept) }
@@ -1110,7 +1110,7 @@ public class KmEffect(
* such as [KmEffectExpression.isNegated].
*/
@ExperimentalContracts
@Suppress("DEPRECATION")
@Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmEffectExpression : KmEffectExpressionVisitor() {
/**
* Effect expression flags, consisting of [Flag.EffectExpression] flags.
@@ -1146,26 +1146,26 @@ public class KmEffectExpression : KmEffectExpressionVisitor() {
*/
public val orArguments: MutableList<KmEffectExpression> = ArrayList(0)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visit(flags: Int, parameterIndex: Int?) {
this.flags = flags
this.parameterIndex = parameterIndex
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstantValue(value: Any?) {
constantValue = KmConstantValue(value)
}
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitIsInstanceType(flags: Int): KmTypeVisitor =
KmType(flags).also { isInstanceType = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAndArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(andArguments)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOrArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(orArguments)
@@ -1174,7 +1174,7 @@ public class KmEffectExpression : KmEffectExpressionVisitor() {
*
* @param visitor the visitor which will visit data in this effect expression
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmEffectExpressionVisitor) {
visitor.visit(flags, parameterIndex)
constantValue?.let { visitor.visitConstantValue(it.value) }
@@ -13,8 +13,8 @@ internal const val VISITOR_API_MESSAGE =
/**
* A visitor containing the common code to visit Kotlin declaration containers, such as classes and package fragments.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmDeclarationContainerVisitor @JvmOverloads constructor(protected open val delegate: KmDeclarationContainerVisitor? = null) {
/**
* Visits a function in the container.
@@ -61,8 +61,8 @@ public abstract class KmDeclarationContainerVisitor @JvmOverloads constructor(pr
* When using this class, [visit] must be called first, followed by zero or more [visitTypeParameter] calls, followed by zero or more calls
* to other visit* methods, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmClassVisitor @JvmOverloads constructor(delegate: KmClassVisitor? = null) : KmDeclarationContainerVisitor(delegate) {
override val delegate: KmClassVisitor?
get() = super.delegate as KmClassVisitor?
@@ -194,8 +194,8 @@ public abstract class KmClassVisitor @JvmOverloads constructor(delegate: KmClass
*
* When using this class, [visitEnd] must be called exactly once and after calls to all other visit* methods.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmPackageVisitor @JvmOverloads constructor(delegate: KmPackageVisitor? = null) :
KmDeclarationContainerVisitor(delegate) {
override val delegate: KmPackageVisitor?
@@ -222,8 +222,8 @@ public abstract class KmPackageVisitor @JvmOverloads constructor(delegate: KmPac
*
* When using this class, [visitFunction] must be called first, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmLambdaVisitor @JvmOverloads constructor(private val delegate: KmLambdaVisitor? = null) {
/**
* Visits the signature of a synthetic anonymous function, representing the lambda.
@@ -247,8 +247,8 @@ public abstract class KmLambdaVisitor @JvmOverloads constructor(private val dele
*
* When using this class, [visitEnd] must be called exactly once and after calls to all other visit* methods.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmConstructorVisitor @JvmOverloads constructor(private val delegate: KmConstructorVisitor? = null) {
/**
* Visits a value parameter of the constructor.
@@ -287,8 +287,8 @@ public abstract class KmConstructorVisitor @JvmOverloads constructor(private val
* When using this class, zero or more calls to [visitTypeParameter] must be done first, followed by zero or more calls
* to other visit* methods, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmFunctionVisitor @JvmOverloads constructor(private val delegate: KmFunctionVisitor? = null) {
/**
* Visits a type parameter of the function.
@@ -371,8 +371,8 @@ public abstract class KmFunctionVisitor @JvmOverloads constructor(private val de
* When using this class, zero or more calls to [visitTypeParameter] must be done first, followed by zero or more calls
* to other visit* methods, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmPropertyVisitor @JvmOverloads constructor(private val delegate: KmPropertyVisitor? = null) {
/**
* Visits a type parameter of the property.
@@ -448,8 +448,8 @@ public abstract class KmPropertyVisitor @JvmOverloads constructor(private val de
* When using this class, zero or more calls to [visitTypeParameter] must be done first, followed by zero or more calls
* to other visit* methods, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmTypeAliasVisitor @JvmOverloads constructor(private val delegate: KmTypeAliasVisitor? = null) {
/**
* Visits a type parameter of the type alias.
@@ -517,8 +517,8 @@ public abstract class KmTypeAliasVisitor @JvmOverloads constructor(private val d
* When using this class, either [visitType] or [visitVarargElementType] must be called first (depending on whether the value parameter
* is `vararg` or not), followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmValueParameterVisitor @JvmOverloads constructor(private val delegate: KmValueParameterVisitor? = null) {
/**
* Visits the type of the value parameter, if this is **not** a `vararg` parameter.
@@ -557,8 +557,8 @@ public abstract class KmValueParameterVisitor @JvmOverloads constructor(private
*
* When using this class, zero or more [visitUpperBound] calls must be done first, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmTypeParameterVisitor @JvmOverloads constructor(private val delegate: KmTypeParameterVisitor? = null) {
/**
* Visits the upper bound of the type parameter.
@@ -595,8 +595,8 @@ public abstract class KmTypeParameterVisitor @JvmOverloads constructor(private v
*
* When using this class, [visitEnd] must be called exactly once and after calls to all other visit* methods.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
public abstract class KmTypeVisitor @JvmOverloads constructor(private val delegate: KmTypeVisitor? = null) {
/**
* Visits the name of the class, if this type's classifier is a class.
@@ -708,8 +708,8 @@ public abstract class KmTypeVisitor @JvmOverloads constructor(private val delega
*
* When using this class, [visit] must be called first, followed by [visitVersion], followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
public abstract class KmVersionRequirementVisitor @JvmOverloads constructor(@Suppress("DEPRECATION") private val delegate: KmVersionRequirementVisitor? = null) {
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public abstract class KmVersionRequirementVisitor @JvmOverloads constructor(@Suppress("DEPRECATION_ERROR") private val delegate: KmVersionRequirementVisitor? = null) {
/**
* Visits the description of this version requirement.
*
@@ -750,8 +750,8 @@ public abstract class KmVersionRequirementVisitor @JvmOverloads constructor(@Sup
*
* When using this class, zero or more calls to [visitEffect] must be done first, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
@ExperimentalContracts
public abstract class KmContractVisitor @JvmOverloads constructor(private val delegate: KmContractVisitor? = null) {
/**
@@ -781,8 +781,8 @@ public abstract class KmContractVisitor @JvmOverloads constructor(private val de
* When using this class, zero or more calls to [visitConstructorArgument] or [visitConclusionOfConditionalEffect] must be done first,
* followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
@ExperimentalContracts
public abstract class KmEffectVisitor @JvmOverloads constructor(private val delegate: KmEffectVisitor? = null) {
/**
@@ -815,8 +815,8 @@ public abstract class KmEffectVisitor @JvmOverloads constructor(private val dele
*
* When using this class, [visit] must be called first, followed by zero or more calls to other visit* methods, followed by [visitEnd].
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION_ERROR")
@ExperimentalContracts
public abstract class KmEffectExpressionVisitor @JvmOverloads constructor(private val delegate: KmEffectExpressionVisitor? = null) {
/**
@@ -2,13 +2,13 @@
* 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION") // flags
package kotlinx.metadata.internal
import kotlinx.metadata.*
import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.common.KmModuleFragmentVisitor
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.*
import kotlin.contracts.ExperimentalContracts
@@ -49,11 +49,11 @@ public class ReadContext(
}
@OptIn(ExperimentalContextReceivers::class)
public fun ProtoBuf.Class.accept(
v: KmClassVisitor,
public fun ProtoBuf.Class.toKmClass(
strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList()
) {
contextExtensions: List<ReadContextExtension> = emptyList(),
): KmClass {
val v = KmClass()
val c = ReadContext(
strings,
TypeTable(typeTable),
@@ -61,61 +61,36 @@ public fun ProtoBuf.Class.accept(
contextExtensions = contextExtensions
).withTypeParameters(typeParameterList)
v.visit(flags, c.className(fqName))
for (typeParameter in typeParameterList) {
typeParameter.accept(v::visitTypeParameter, c)
}
for (supertype in supertypes(c.types)) {
v.visitSupertype(supertype.typeFlags)?.let { supertype.accept(it, c) }
}
for (constructor in constructorList) {
v.visitConstructor(constructor.flags)?.let { constructor.accept(it, c) }
}
v.flags = flags
v.name = c.className(fqName)
typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
supertypes(c.types).mapTo(v.supertypes) { it.toKmType(c) }
constructorList.mapTo(v.constructors) { it.toKmConstructor(c) }
v.visitDeclarations(functionList, propertyList, typeAliasList, c)
if (hasCompanionObjectName()) {
v.visitCompanionObject(c[companionObjectName])
}
for (nestedClassName in nestedClassNameList) {
v.visitNestedClass(c[nestedClassName])
v.companionObject = c[companionObjectName]
}
nestedClassNameList.mapTo(v.nestedClasses) { c[it] }
for (enumEntry in enumEntryList) {
if (!enumEntry.hasName()) {
throw InconsistentKotlinMetadataException("No name for EnumEntry")
}
v.visitEnumEntry(c[enumEntry.name])
if (!enumEntry.hasName()) throw InconsistentKotlinMetadataException("No name for EnumEntry")
v.enumEntries.add(c[enumEntry.name])
}
for (sealedSubclassFqName in sealedSubclassFqNameList) {
v.visitSealedSubclass(c.className(sealedSubclassFqName))
}
sealedSubclassFqNameList.mapTo(v.sealedSubclasses) { c.className(it) }
if (hasInlineClassUnderlyingPropertyName()) {
v.visitInlineClassUnderlyingPropertyName(c[inlineClassUnderlyingPropertyName])
}
loadInlineClassUnderlyingType(c)?.let { underlyingType ->
v.visitInlineClassUnderlyingType(underlyingType.typeFlags)?.let { underlyingType.accept(it, c) }
v.inlineClassUnderlyingPropertyName = c[inlineClassUnderlyingPropertyName]
}
v.inlineClassUnderlyingType = loadInlineClassUnderlyingType(c)?.toKmType(c)
for (contextReceiverType in contextReceiverTypes(c.types)) {
v.visitContextReceiverType(contextReceiverType.typeFlags)?.let { contextReceiverType.accept(it, c) }
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(c) }
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) {
extension.readClassExtensions(v, this, c)
}
v.visitEnd()
return v
}
private fun ProtoBuf.Class.loadInlineClassUnderlyingType(c: ReadContext): ProtoBuf.Type? {
@@ -130,11 +105,11 @@ private fun ProtoBuf.Class.loadInlineClassUnderlyingType(c: ReadContext): ProtoB
?.returnType(c.types)
}
public fun ProtoBuf.Package.accept(
v: KmPackageVisitor,
public fun ProtoBuf.Package.toKmPackage(
strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList()
) {
contextExtensions: List<ReadContextExtension> = emptyList(),
): KmPackage {
val v = KmPackage()
val c = ReadContext(
strings,
TypeTable(typeTable),
@@ -148,14 +123,14 @@ public fun ProtoBuf.Package.accept(
extension.readPackageExtensions(v, this, c)
}
v.visitEnd()
return v
}
public fun ProtoBuf.PackageFragment.accept(
v: KmModuleFragmentVisitor,
public fun ProtoBuf.PackageFragment.toKmModuleFragment(
strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList()
) {
contextExtensions: List<ReadContextExtension> = emptyList(),
): KmModuleFragment {
val v = KmModuleFragment()
val c = ReadContext(
strings,
TypeTable(ProtoBuf.TypeTable.newBuilder().build()),
@@ -163,223 +138,155 @@ public fun ProtoBuf.PackageFragment.accept(
contextExtensions = contextExtensions
)
v.visitPackage()?.let { `package`.accept(it, strings, contextExtensions) }
class_List.forEach { clazz ->
v.visitClass()?.let { clazz.accept(it, strings, contextExtensions) }
}
v.pkg = `package`.toKmPackage(strings, contextExtensions)
class_List.mapTo(v.classes) { it.toKmClass(strings, contextExtensions) }
for (extension in c.extensions) {
extension.readModuleFragmentExtensions(v, this, c)
}
v.visitEnd()
return v
}
private fun KmDeclarationContainerVisitor.visitDeclarations(
functions: List<ProtoBuf.Function>,
properties: List<ProtoBuf.Property>,
typeAliases: List<ProtoBuf.TypeAlias>,
c: ReadContext
private fun KmDeclarationContainer.visitDeclarations(
protoFunctions: List<ProtoBuf.Function>,
protoProperties: List<ProtoBuf.Property>,
protoTypeAliases: List<ProtoBuf.TypeAlias>,
c: ReadContext,
) {
for (function in functions) {
visitFunction(function.flags, c[function.name])?.let { function.accept(it, c) }
}
for (property in properties) {
visitProperty(
property.flags, c[property.name], property.getPropertyGetterFlags(), property.getPropertySetterFlags()
)?.let { property.accept(it, c) }
}
for (typeAlias in typeAliases) {
visitTypeAlias(typeAlias.flags, c[typeAlias.name])?.let { typeAlias.accept(it, c) }
}
protoFunctions.mapTo(functions) { it.toKmFunction(c) }
protoProperties.mapTo(properties) { it.toKmProperty(c) }
protoTypeAliases.mapTo(typeAliases) { it.toKmTypeAlias(c) }
}
public fun ProtoBuf.Function.accept(v: KmLambdaVisitor, strings: NameResolver) {
public fun ProtoBuf.Function.toKmLambda(strings: NameResolver): KmLambda {
val v = KmLambda()
val c = ReadContext(strings, TypeTable(typeTable), VersionRequirementTable.EMPTY)
v.visitFunction(flags, c[name])?.let { accept(it, c) }
v.visitEnd()
v.function = this.toKmFunction(c)
return v
}
private fun ProtoBuf.Constructor.accept(v: KmConstructorVisitor, c: ReadContext) {
for (parameter in valueParameterList) {
v.visitValueParameter(parameter.flags, c[parameter.name])?.let { parameter.accept(it, c) }
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
private fun ProtoBuf.Constructor.toKmConstructor(c: ReadContext): KmConstructor {
val v = KmConstructor(flags)
valueParameterList.mapTo(v.valueParameters) { it.toKmValueParameter(c) }
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) {
extension.readConstructorExtensions(v, this, c)
}
v.visitEnd()
return v
}
@OptIn(ExperimentalContextReceivers::class)
private fun ProtoBuf.Function.accept(v: KmFunctionVisitor, outer: ReadContext) {
private fun ProtoBuf.Function.toKmFunction(outer: ReadContext): KmFunction {
val v = KmFunction(flags, outer[name])
val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) {
typeParameter.accept(v::visitTypeParameter, c)
}
receiverType(c.types)?.let { receiverType ->
v.visitReceiverParameterType(receiverType.typeFlags)?.let { receiverType.accept(it, c) }
}
for (contextReceiverType in contextReceiverTypes(c.types)) {
v.visitContextReceiverType(contextReceiverType.typeFlags)?.let { contextReceiverType.accept(it, c) }
}
for (parameter in valueParameterList) {
v.visitValueParameter(parameter.flags, c[parameter.name])?.let { parameter.accept(it, c) }
}
returnType(c.types).let { returnType ->
v.visitReturnType(returnType.typeFlags)?.let { returnType.accept(it, c) }
}
typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
v.receiverParameterType = receiverType(c.types)?.toKmType(c)
contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(c) }
valueParameterList.mapTo(v.valueParameters) { it.toKmValueParameter(c) }
v.returnType = returnType(c.types).toKmType(c)
@OptIn(ExperimentalContracts::class)
if (hasContract()) {
@OptIn(ExperimentalContracts::class) v.visitContract()?.let { contract.accept(it, c) }
v.contract = contract.toKmContract(c)
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) {
extension.readFunctionExtensions(v, this, c)
}
v.visitEnd()
return v
}
@OptIn(ExperimentalContextReceivers::class)
public fun ProtoBuf.Property.accept(v: KmPropertyVisitor, outer: ReadContext) {
public fun ProtoBuf.Property.toKmProperty(outer: ReadContext): KmProperty {
val v = KmProperty(flags, outer[name], getPropertyGetterFlags(), getPropertySetterFlags())
val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) {
typeParameter.accept(v::visitTypeParameter, c)
}
receiverType(c.types)?.let { receiverType ->
v.visitReceiverParameterType(receiverType.typeFlags)?.let { receiverType.accept(it, c) }
}
for (contextReceiverType in contextReceiverTypes(c.types)) {
v.visitContextReceiverType(contextReceiverType.typeFlags)?.let { contextReceiverType.accept(it, c) }
}
typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
v.receiverParameterType = receiverType(c.types)?.toKmType(c)
contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(c) }
if (hasSetterValueParameter()) {
val parameter = setterValueParameter
v.visitSetterParameter(parameter.flags, c[parameter.name])?.let { parameter.accept(it, c) }
}
returnType(c.types).let { returnType ->
v.visitReturnType(returnType.typeFlags)?.let { returnType.accept(it, c) }
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
v.setterParameter = setterValueParameter.toKmValueParameter(c)
}
v.returnType = returnType(c.types).toKmType(c)
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) {
extension.readPropertyExtensions(v, this, c)
}
v.visitEnd()
return v
}
private fun ProtoBuf.TypeAlias.accept(v: KmTypeAliasVisitor, outer: ReadContext) {
private fun ProtoBuf.TypeAlias.toKmTypeAlias(outer: ReadContext): KmTypeAlias {
val v = KmTypeAlias(flags, outer[name])
val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) {
typeParameter.accept(v::visitTypeParameter, c)
}
typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
v.underlyingType = underlyingType(c.types).toKmType(c)
v.expandedType = expandedType(c.types).toKmType(c)
annotationList.mapTo(v.annotations) { it.readAnnotation(c.strings) }
underlyingType(c.types).let { underlyingType ->
v.visitUnderlyingType(underlyingType.typeFlags)?.let { underlyingType.accept(it, c) }
}
expandedType(c.types).let { expandedType ->
v.visitExpandedType(expandedType.typeFlags)?.let { expandedType.accept(it, c) }
}
for (annotation in annotationList) {
v.visitAnnotation(annotation.readAnnotation(c.strings))
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) {
extension.readTypeAliasExtensions(v, this, c)
}
v.visitEnd()
return v
}
private fun ProtoBuf.ValueParameter.accept(v: KmValueParameterVisitor, c: ReadContext) {
type(c.types).let { type ->
v.visitType(type.typeFlags)?.let { type.accept(it, c) }
}
private fun ProtoBuf.ValueParameter.toKmValueParameter(c: ReadContext): KmValueParameter {
val v = KmValueParameter(flags, c[name])
v.type = type(c.types).toKmType(c)
varargElementType(c.types)?.let { varargElementType ->
v.visitVarargElementType(varargElementType.typeFlags)?.let { varargElementType.accept(it, c) }
}
v.varargElementType = varargElementType(c.types)?.toKmType(c)
for (extension in c.extensions) {
extension.readValueParameterExtensions(v, this, c)
}
v.visitEnd()
return v
}
private inline fun ProtoBuf.TypeParameter.accept(
visit: (flags: Int, name: String, id: Int, variance: KmVariance) -> KmTypeParameterVisitor?,
c: ReadContext
) {
private fun ProtoBuf.TypeParameter.toKmTypeParameter(
c: ReadContext,
): KmTypeParameter {
val variance = when (requireNotNull(variance)) {
ProtoBuf.TypeParameter.Variance.IN -> KmVariance.IN
ProtoBuf.TypeParameter.Variance.OUT -> KmVariance.OUT
ProtoBuf.TypeParameter.Variance.INV -> KmVariance.INVARIANT
}
val ktp = KmTypeParameter(typeParameterFlags, c[name], id, variance)
visit(typeParameterFlags, c[name], id, variance)?.let { accept(it, c) }
}
private fun ProtoBuf.TypeParameter.accept(v: KmTypeParameterVisitor, c: ReadContext) {
for (upperBound in upperBounds(c.types)) {
v.visitUpperBound(upperBound.typeFlags)?.let { upperBound.accept(it, c) }
}
upperBounds(c.types).mapTo(ktp.upperBounds) { it.toKmType(c) }
for (extension in c.extensions) {
extension.readTypeParameterExtensions(v, this, c)
extension.readTypeParameterExtensions(ktp, this, c)
}
v.visitEnd()
return ktp
}
private fun ProtoBuf.Type.accept(v: KmTypeVisitor, c: ReadContext) {
when {
hasClassName() -> v.visitClass(c.className(className))
hasTypeAliasName() -> v.visitTypeAlias(c.className(typeAliasName))
hasTypeParameter() -> v.visitTypeParameter(typeParameter)
private fun ProtoBuf.Type.toKmType(c: ReadContext): KmType {
val v = KmType(typeFlags)
v.classifier = when {
hasClassName() -> KmClassifier.Class(c.className(className))
hasTypeAliasName() -> KmClassifier.TypeAlias(c.className(typeAliasName))
hasTypeParameter() -> KmClassifier.TypeParameter(typeParameter)
hasTypeParameterName() -> {
val id = c.getTypeParameterId(typeParameterName)
?: throw InconsistentKotlinMetadataException("No type parameter id for ${c[typeParameterName]}")
v.visitTypeParameter(id)
}
else -> {
throw InconsistentKotlinMetadataException("No classifier (class, type alias or type parameter) recorded for Type")
KmClassifier.TypeParameter(id)
}
else -> throw InconsistentKotlinMetadataException("No classifier (class, type alias or type parameter) recorded for Type")
}
for (argument in argumentList) {
@@ -393,35 +300,28 @@ private fun ProtoBuf.Type.accept(v: KmTypeVisitor, c: ReadContext) {
if (variance != null) {
val argumentType = argument.type(c.types)
?: throw InconsistentKotlinMetadataException("No type argument for non-STAR projection in Type")
v.visitArgument(argumentType.typeFlags, variance)?.let { argumentType.accept(it, c) }
v.arguments.add(KmTypeProjection(variance, argumentType.toKmType(c)))
} else {
v.visitStarProjection()
v.arguments.add(KmTypeProjection.STAR)
}
}
abbreviatedType(c.types)?.let { abbreviatedType ->
v.visitAbbreviatedType(abbreviatedType.typeFlags)?.let { abbreviatedType.accept(it, c) }
}
v.abbreviatedType = abbreviatedType(c.types)?.toKmType(c)
v.outerType = outerType(c.types)?.toKmType(c)
outerType(c.types)?.let { outerType ->
v.visitOuterType(outerType.typeFlags)?.let { outerType.accept(it, c) }
}
flexibleUpperBound(c.types)?.let { upperBound ->
v.visitFlexibleTypeUpperBound(
upperBound.typeFlags,
if (hasFlexibleTypeCapabilitiesId()) c[flexibleTypeCapabilitiesId] else null
)?.let { upperBound.accept(it, c) }
v.flexibleTypeUpperBound = flexibleUpperBound(c.types)?.toKmType(c)?.let {
KmFlexibleTypeUpperBound(it, if (hasFlexibleTypeCapabilitiesId()) c[flexibleTypeCapabilitiesId] else null)
}
for (extension in c.extensions) {
extension.readTypeExtensions(v, this, c)
}
v.visitEnd()
return v
}
private fun acceptVersionRequirementVisitor(id: Int, v: KmVersionRequirementVisitor, c: ReadContext) {
private fun readVersionRequirement(id: Int, c: ReadContext): KmVersionRequirement {
val v = KmVersionRequirement()
val message = VersionRequirement.create(id, c.strings, c.versionRequirements)
?: throw InconsistentKotlinMetadataException("No VersionRequirement with the given id in the table")
@@ -437,16 +337,19 @@ private fun acceptVersionRequirementVisitor(id: Int, v: KmVersionRequirementVisi
DeprecationLevel.HIDDEN -> KmVersionRequirementLevel.HIDDEN
}
v.visit(kind, level, message.errorCode, message.message)
v.kind = kind
v.level = level
v.errorCode = message.errorCode
v.message = message.message
val (major, minor, patch) = message.version
v.visitVersion(major, minor, patch)
v.visitEnd()
v.version = KmVersion(major, minor, patch)
return v
}
@ExperimentalContracts
private fun ProtoBuf.Contract.accept(v: KmContractVisitor, c: ReadContext) {
private fun ProtoBuf.Contract.toKmContract(c: ReadContext): KmContract {
val v = KmContract()
for (effect in effectList) {
if (!effect.hasEffectType()) continue
@@ -462,34 +365,32 @@ private fun ProtoBuf.Contract.accept(v: KmContractVisitor, c: ReadContext) {
ProtoBuf.Effect.InvocationKind.AT_LEAST_ONCE -> KmEffectInvocationKind.AT_LEAST_ONCE
}
v.visitEffect(effectType, effectKind)?.let { effect.accept(it, c) }
v.effects.add(effect.toKmEffect(effectType, effectKind, c))
}
v.visitEnd()
return v
}
@ExperimentalContracts
private fun ProtoBuf.Effect.accept(v: KmEffectVisitor, c: ReadContext) {
for (constructorArgument in effectConstructorArgumentList) {
v.visitConstructorArgument()?.let { constructorArgument.accept(it, c) }
}
private fun ProtoBuf.Effect.toKmEffect(type: KmEffectType, kind: KmEffectInvocationKind?, c: ReadContext): KmEffect {
val v = KmEffect(type, kind)
effectConstructorArgumentList.mapTo(v.constructorArguments) { it.toKmEffectExpression(c) }
if (hasConclusionOfConditionalEffect()) {
v.visitConclusionOfConditionalEffect()?.let { conclusionOfConditionalEffect.accept(it, c) }
v.conclusion = conclusionOfConditionalEffect.toKmEffectExpression(c)
}
v.visitEnd()
return v
}
@ExperimentalContracts
private fun ProtoBuf.Expression.accept(v: KmEffectExpressionVisitor, c: ReadContext) {
v.visit(
flags,
if (hasValueParameterReference()) valueParameterReference else null
)
private fun ProtoBuf.Expression.toKmEffectExpression(c: ReadContext): KmEffectExpression {
val v = KmEffectExpression()
v.flags = flags
v.parameterIndex = if (hasValueParameterReference()) valueParameterReference else null
if (hasConstantValue()) {
v.visitConstantValue(
v.constantValue = KmConstantValue(
when (requireNotNull(constantValue)) {
ProtoBuf.Expression.ConstantValue.TRUE -> true
ProtoBuf.Expression.ConstantValue.FALSE -> false
@@ -498,19 +399,12 @@ private fun ProtoBuf.Expression.accept(v: KmEffectExpressionVisitor, c: ReadCont
)
}
isInstanceType(c.types)?.let { type ->
v.visitIsInstanceType(type.typeFlags)?.let { type.accept(it, c) }
}
v.isInstanceType = isInstanceType(c.types)?.toKmType(c)
for (andArgument in andArgumentList) {
v.visitAndArgument()?.let { andArgument.accept(it, c) }
}
andArgumentList.mapTo(v.andArguments) { it.toKmEffectExpression(c) }
orArgumentList.mapTo(v.orArguments) { it.toKmEffectExpression(c) }
for (orArgument in orArgumentList) {
v.visitOrArgument()?.let { orArgument.accept(it, c) }
}
v.visitEnd()
return v
}
private val ProtoBuf.Type.typeFlags: Int
@@ -5,10 +5,10 @@
package kotlinx.metadata.internal
import kotlinx.metadata.ClassName
import kotlinx.metadata.KmAnnotation
import kotlinx.metadata.KmAnnotationArgument
import kotlinx.metadata.isLocalClassName
import kotlinx.metadata.*
import kotlinx.metadata.internal.extensions.KmExtension
import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.extensions.applySingleExtension
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.Flags
import org.jetbrains.kotlin.metadata.serialization.StringTable
@@ -115,3 +115,9 @@ internal fun StringTable.getClassNameIndex(name: ClassName): Int =
getQualifiedClassNameIndex(name.substring(1), true)
else
getQualifiedClassNameIndex(name, false)
@Suppress("DEPRECATION_ERROR")
internal fun <V : KmExtensionVisitor, E : KmExtension<V>> List<E>.writeExtensions(writer: MetadataExtensions.(KmExtensionType) -> V?) =
forEach { ext ->
applySingleExtension(ext.type) { writer(ext.type) }?.let { ext.accept(it) }
}
@@ -2,13 +2,13 @@
* Copyright 2010-2022 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION") // flags
package kotlinx.metadata.internal
import kotlinx.metadata.*
import kotlinx.metadata.internal.common.KmModuleFragmentExtensionVisitor
import kotlinx.metadata.internal.common.KmModuleFragmentVisitor
import kotlinx.metadata.internal.extensions.applySingleExtension
import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.extensions.matchExtensions
import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.VersionRequirement
import org.jetbrains.kotlin.metadata.serialization.MutableVersionRequirementTable
@@ -31,526 +31,348 @@ public open class WriteContext(public val strings: StringTable, public val conte
strings.getClassNameIndex(name)
}
private fun writeTypeParameter(
c: WriteContext, flags: Int, name: String, id: Int, variance: KmVariance,
output: (ProtoBuf.TypeParameter.Builder) -> Unit
): KmTypeParameterVisitor =
object : KmTypeParameterVisitor() {
private val t = ProtoBuf.TypeParameter.newBuilder()
private fun WriteContext.writeTypeParameter(kmTypeParameter: KmTypeParameter): ProtoBuf.TypeParameter.Builder {
val t = ProtoBuf.TypeParameter.newBuilder()
kmTypeParameter.upperBounds.forEach { ub ->
t.addUpperBound(writeType(ub).build())
}
kmTypeParameter.extensions.writeExtensions {
writeTypeParameterExtensions(it, t, this@writeTypeParameter)
}
t.name = this[kmTypeParameter.name]
t.id = kmTypeParameter.id
val reified = kmTypeParameter.isReified
if (reified != ProtoBuf.TypeParameter.getDefaultInstance().reified) {
t.reified = reified
}
if (kmTypeParameter.variance == KmVariance.IN) {
t.variance = ProtoBuf.TypeParameter.Variance.IN
} else if (kmTypeParameter.variance == KmVariance.OUT) {
t.variance = ProtoBuf.TypeParameter.Variance.OUT
}
return t
}
override fun visitUpperBound(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.addUpperBound(it) }
override fun visitExtensions(type: KmExtensionType): KmTypeParameterExtensionVisitor? =
applySingleExtension(type) {
writeTypeParameterExtensions(type, t, c)
}
override fun visitEnd() {
t.name = c[name]
t.id = id
val reified = Flag.TypeParameter.IS_REIFIED(flags)
if (reified != ProtoBuf.TypeParameter.getDefaultInstance().reified) {
t.reified = reified
}
if (variance == KmVariance.IN) {
t.variance = ProtoBuf.TypeParameter.Variance.IN
} else if (variance == KmVariance.OUT) {
t.variance = ProtoBuf.TypeParameter.Variance.OUT
}
output(t)
private fun WriteContext.writeTypeProjection(argument: KmTypeProjection): ProtoBuf.Type.Argument.Builder {
val t = ProtoBuf.Type.Argument.newBuilder()
if (argument == KmTypeProjection.STAR) {
t.projection = ProtoBuf.Type.Argument.Projection.STAR
} else {
val (variance, argType) = argument
if (variance == null || argType == null)
throw InconsistentKotlinMetadataException("Variance and type must be set for non-star type projection")
if (variance == KmVariance.IN) {
t.projection = ProtoBuf.Type.Argument.Projection.IN
} else if (variance == KmVariance.OUT) {
t.projection = ProtoBuf.Type.Argument.Projection.OUT
}
t.type = writeType(argType).build()
}
private fun writeType(c: WriteContext, flags: Int, output: (ProtoBuf.Type.Builder) -> Unit): KmTypeVisitor =
object : KmTypeVisitor() {
private val t = ProtoBuf.Type.newBuilder()
return t
}
override fun visitClass(name: ClassName) {
t.className = c.getClassName(name)
private fun WriteContext.writeType(kmType: KmType): ProtoBuf.Type.Builder {
val t = ProtoBuf.Type.newBuilder()
when (val cls = kmType.classifier) {
is KmClassifier.Class -> t.className = getClassName(cls.name)
is KmClassifier.TypeAlias -> t.typeAliasName = getClassName(cls.name)
is KmClassifier.TypeParameter -> t.typeParameter = cls.id
}
kmType.arguments.forEach { argument ->
t.addArgument(writeTypeProjection(argument))
}
kmType.abbreviatedType?.let { t.abbreviatedType = writeType(it).build() }
kmType.outerType?.let { t.outerType = writeType(it).build() }
kmType.flexibleTypeUpperBound?.let { fub ->
val fubType = writeType(fub.type)
fub.typeFlexibilityId?.let { t.flexibleTypeCapabilitiesId = this[it] }
t.flexibleUpperBound = fubType.build()
}
kmType.extensions.writeExtensions {
writeTypeExtensions(it, t, this@writeType)
}
if (kmType.isNullable) {
t.nullable = true
}
val flagsToWrite = kmType.flags shr 1
if (flagsToWrite != ProtoBuf.Type.getDefaultInstance().flags) {
t.flags = flagsToWrite
}
return t
}
private fun WriteContext.writeConstructor(kmConstructor: KmConstructor): ProtoBuf.Constructor.Builder {
val t = ProtoBuf.Constructor.newBuilder()
kmConstructor.valueParameters.forEach {
t.addValueParameter(writeValueParameter(it).build())
}
kmConstructor.versionRequirements.forEach { t.addVersionRequirement(writeVersionRequirement(it)) }
kmConstructor.extensions.writeExtensions {
writeConstructorExtensions(it, t, this@writeConstructor)
}
if (kmConstructor.flags != ProtoBuf.Constructor.getDefaultInstance().flags) {
t.flags = kmConstructor.flags
}
return t
}
private fun WriteContext.writeFunction(kmFunction: KmFunction): ProtoBuf.Function.Builder {
val t = ProtoBuf.Function.newBuilder()
t.addAllTypeParameter(kmFunction.typeParameters.map { writeTypeParameter(it).build() })
kmFunction.receiverParameterType?.let { t.receiverType = writeType(it).build() }
@OptIn(ExperimentalContextReceivers::class)
t.addAllContextReceiverType(kmFunction.contextReceiverTypes.map { writeType(it).build() })
t.addAllValueParameter(kmFunction.valueParameters.map { writeValueParameter(it).build() })
t.returnType = writeType(kmFunction.returnType).build()
t.addAllVersionRequirement(kmFunction.versionRequirements.map(::writeVersionRequirement))
@OptIn(ExperimentalContracts::class)
kmFunction.contract?.let { t.contract = writeContract(it) }
kmFunction.extensions.writeExtensions { writeFunctionExtensions(it, t, this@writeFunction) }
t.name = this[kmFunction.name]
if (kmFunction.flags != ProtoBuf.Function.getDefaultInstance().flags) {
t.flags = kmFunction.flags
}
return t
}
public fun WriteContext.writeProperty(kmProperty: KmProperty): ProtoBuf.Property.Builder {
val t = ProtoBuf.Property.newBuilder()
kmProperty.typeParameters.forEach { tp ->
t.addTypeParameter(writeTypeParameter(tp).build())
}
kmProperty.receiverParameterType?.let { t.receiverType = writeType(it).build() }
@OptIn(ExperimentalContextReceivers::class)
t.addAllContextReceiverType(kmProperty.contextReceiverTypes.map { writeType(it).build() })
kmProperty.setterParameter?.let { t.setterValueParameter = writeValueParameter(it).build() }
t.returnType = writeType(kmProperty.returnType).build()
t.addAllVersionRequirement(kmProperty.versionRequirements.map { writeVersionRequirement(it) })
kmProperty.extensions.writeExtensions { writePropertyExtensions(it, t, this@writeProperty) }
t.name = this[kmProperty.name]
if (kmProperty.flags != ProtoBuf.Property.getDefaultInstance().flags) {
t.flags = kmProperty.flags
}
// TODO: do not write getterFlags/setterFlags if not needed
if (kmProperty.hasGetter) t.getterFlags = kmProperty.getterFlags
if (kmProperty.hasSetter) t.setterFlags = kmProperty.setterFlags
return t
}
private fun WriteContext.writeValueParameter(
kmValueParameter: KmValueParameter,
): ProtoBuf.ValueParameter.Builder {
val t = ProtoBuf.ValueParameter.newBuilder()
t.type = writeType(kmValueParameter.type).build()
kmValueParameter.varargElementType?.let { t.varargElementType = writeType(it).build() }
kmValueParameter.extensions.writeExtensions { writeValueParameterExtensions(it, t, this@writeValueParameter) }
if (kmValueParameter.flags != ProtoBuf.ValueParameter.getDefaultInstance().flags) {
t.flags = kmValueParameter.flags
}
t.name = this[kmValueParameter.name]
return t
}
private fun WriteContext.writeTypeAlias(
typeAlias: KmTypeAlias,
): ProtoBuf.TypeAlias.Builder {
val t = ProtoBuf.TypeAlias.newBuilder()
t.addAllTypeParameter(typeAlias.typeParameters.map { writeTypeParameter(it).build() })
t.underlyingType = writeType(typeAlias.underlyingType).build()
t.expandedType = writeType(typeAlias.expandedType).build()
t.addAllAnnotation(typeAlias.annotations.map { it.writeAnnotation(strings).build() })
t.addAllVersionRequirement(typeAlias.versionRequirements.map(::writeVersionRequirement))
typeAlias.extensions.writeExtensions { writeTypeAliasExtensions(it, t, this@writeTypeAlias) }
if (typeAlias.flags != ProtoBuf.TypeAlias.getDefaultInstance().flags) {
t.flags = typeAlias.flags
}
t.name = this[typeAlias.name]
return t
}
private fun WriteContext.writeVersionRequirement(kmVersionRequirement: KmVersionRequirement): Int {
val kind = kmVersionRequirement.kind
val level = kmVersionRequirement.level
val errorCode = kmVersionRequirement.errorCode
val message = kmVersionRequirement.message
val t = ProtoBuf.VersionRequirement.newBuilder().apply {
val versionKind = when (kind) {
KmVersionRequirementVersionKind.LANGUAGE_VERSION -> ProtoBuf.VersionRequirement.VersionKind.LANGUAGE_VERSION
KmVersionRequirementVersionKind.COMPILER_VERSION -> ProtoBuf.VersionRequirement.VersionKind.COMPILER_VERSION
KmVersionRequirementVersionKind.API_VERSION -> ProtoBuf.VersionRequirement.VersionKind.API_VERSION
}
override fun visitTypeAlias(name: ClassName) {
t.typeAliasName = c.getClassName(name)
if (versionKind != defaultInstanceForType.versionKind) {
this.versionKind = versionKind
}
val requirementLevel = when (level) {
KmVersionRequirementLevel.WARNING -> ProtoBuf.VersionRequirement.Level.WARNING
KmVersionRequirementLevel.ERROR -> ProtoBuf.VersionRequirement.Level.ERROR
KmVersionRequirementLevel.HIDDEN -> ProtoBuf.VersionRequirement.Level.HIDDEN
}
if (requirementLevel != defaultInstanceForType.level) {
this.level = requirementLevel
}
if (errorCode != null) {
this.errorCode = errorCode
}
if (message != null) {
this.message = this@writeVersionRequirement[message]
}
}
val (major, minor, patch) = kmVersionRequirement.version
override fun visitStarProjection() {
t.addArgument(ProtoBuf.Type.Argument.newBuilder().apply {
projection = ProtoBuf.Type.Argument.Projection.STAR
VersionRequirement.Version(major, minor, patch).encode(
writeVersion = { t!!.version = it },
writeVersionFull = { t!!.versionFull = it }
)
return this.versionRequirements[t] //??
}
@ExperimentalContracts
private fun WriteContext.writeContract(contract: KmContract): ProtoBuf.Contract {
val t = ProtoBuf.Contract.newBuilder()
t.addAllEffect(contract.effects.map(::writeEffect))
return t.build()
}
@ExperimentalContracts
private fun WriteContext.writeEffect(
effect: KmEffect,
): ProtoBuf.Effect {
val t = ProtoBuf.Effect.newBuilder()
t.addAllEffectConstructorArgument(effect.constructorArguments.map(::writeEffectExpression))
effect.conclusion?.let { t.conclusionOfConditionalEffect = writeEffectExpression(it) }
when (effect.type) {
KmEffectType.RETURNS_CONSTANT -> t.effectType = ProtoBuf.Effect.EffectType.RETURNS_CONSTANT
KmEffectType.CALLS -> t.effectType = ProtoBuf.Effect.EffectType.CALLS
KmEffectType.RETURNS_NOT_NULL -> t.effectType = ProtoBuf.Effect.EffectType.RETURNS_NOT_NULL
}
when (effect.invocationKind) {
KmEffectInvocationKind.AT_MOST_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.AT_MOST_ONCE
KmEffectInvocationKind.EXACTLY_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.EXACTLY_ONCE
KmEffectInvocationKind.AT_LEAST_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.AT_LEAST_ONCE
null -> {} // nop
}
return t.build()
}
@ExperimentalContracts
private fun WriteContext.writeEffectExpression(effectExpression: KmEffectExpression): ProtoBuf.Expression {
val t = ProtoBuf.Expression.newBuilder()
if (effectExpression.flags != ProtoBuf.Expression.getDefaultInstance().flags) {
t.flags = effectExpression.flags
}
effectExpression.parameterIndex?.let { t.valueParameterReference = it }
val cv = effectExpression.constantValue
if (cv != null) {
when (val value = cv.value) {
true -> t.constantValue = ProtoBuf.Expression.ConstantValue.TRUE
false -> t.constantValue = ProtoBuf.Expression.ConstantValue.FALSE
null -> t.constantValue = ProtoBuf.Expression.ConstantValue.NULL
else -> throw IllegalArgumentException("Only true, false or null constant values are allowed for effects (was=$value)")
}
}
effectExpression.isInstanceType?.let { t.isInstanceType = writeType(it).build() }
t.addAllAndArgument(effectExpression.andArguments.map(::writeEffectExpression))
t.addAllOrArgument(effectExpression.orArguments.map(::writeEffectExpression))
return t.build()
}
public open class ClassWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) {
public val t: ProtoBuf.Class.Builder = ProtoBuf.Class.newBuilder()!!
public val c: WriteContext = WriteContext(stringTable, contextExtensions)
public fun writeClass(kmClass: KmClass) {
if (kmClass.flags != ProtoBuf.Class.getDefaultInstance().flags) {
t.flags = kmClass.flags
}
t.fqName = c.getClassName(kmClass.name)
t.addAllTypeParameter(kmClass.typeParameters.map { c.writeTypeParameter(it).build() })
t.addAllSupertype(kmClass.supertypes.map { c.writeType(it).build() })
t.addAllConstructor(kmClass.constructors.map { c.writeConstructor(it).build() })
t.addAllFunction(kmClass.functions.map { c.writeFunction(it).build() })
t.addAllProperty(kmClass.properties.map { c.writeProperty(it).build() })
t.addAllTypeAlias(kmClass.typeAliases.map { c.writeTypeAlias(it).build() })
kmClass.companionObject?.let { t.companionObjectName = c[it] }
kmClass.nestedClasses.forEach { t.addNestedClassName(c[it]) }
kmClass.enumEntries.forEach { name ->
t.addEnumEntry(ProtoBuf.EnumEntry.newBuilder().also { enumEntry ->
enumEntry.name = c[name]
})
}
override fun visitArgument(flags: Int, variance: KmVariance): KmTypeVisitor? =
writeType(c, flags) { argument ->
t.addArgument(ProtoBuf.Type.Argument.newBuilder().apply {
if (variance == KmVariance.IN) {
projection = ProtoBuf.Type.Argument.Projection.IN
} else if (variance == KmVariance.OUT) {
projection = ProtoBuf.Type.Argument.Projection.OUT
}
type = argument.build()
})
}
t.addAllSealedSubclassFqName(kmClass.sealedSubclasses.map { c.getClassName(it) })
override fun visitTypeParameter(id: Int) {
t.typeParameter = id
}
kmClass.inlineClassUnderlyingPropertyName?.let { t.inlineClassUnderlyingPropertyName = c[it] }
kmClass.inlineClassUnderlyingType?.let { t.inlineClassUnderlyingType = c.writeType(it).build() }
override fun visitAbbreviatedType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.abbreviatedType = it.build() }
@OptIn(ExperimentalContextReceivers::class)
t.addAllContextReceiverType(kmClass.contextReceiverTypes.map { c.writeType(it).build() })
override fun visitOuterType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.outerType = it.build() }
t.addAllVersionRequirement(kmClass.versionRequirements.map { c.writeVersionRequirement(it) })
kmClass.extensions.matchExtensions { writeClassExtensions(it, t, c) }
override fun visitFlexibleTypeUpperBound(flags: Int, typeFlexibilityId: String?): KmTypeVisitor? =
writeType(c, flags) {
if (typeFlexibilityId != null) {
t.flexibleTypeCapabilitiesId = c[typeFlexibilityId]
}
t.flexibleUpperBound = it.build()
}
override fun visitExtensions(type: KmExtensionType): KmTypeExtensionVisitor? =
applySingleExtension(type) {
writeTypeExtensions(type, t, c)
}
override fun visitEnd() {
if (Flag.Type.IS_NULLABLE(flags)) {
t.nullable = true
}
val flagsToWrite = flags shr 1
if (flagsToWrite != ProtoBuf.Type.getDefaultInstance().flags) {
t.flags = flagsToWrite
}
output(t)
}
}
private fun writeConstructor(c: WriteContext, flags: Int, output: (ProtoBuf.Constructor.Builder) -> Unit): KmConstructorVisitor =
object : KmConstructorVisitor() {
val t = ProtoBuf.Constructor.newBuilder()
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor? =
writeValueParameter(c, flags, name) { t.addValueParameter(it.build()) }
override fun visitVersionRequirement(): KmVersionRequirementVisitor? =
writeVersionRequirement(c) { t.addVersionRequirement(it) }
override fun visitExtensions(type: KmExtensionType): KmConstructorExtensionVisitor? =
applySingleExtension(type) {
writeConstructorExtensions(type, t, c)
}
override fun visitEnd() {
if (flags != ProtoBuf.Constructor.getDefaultInstance().flags) {
t.flags = flags
}
output(t)
}
}
private fun writeFunction(c: WriteContext, flags: Int, name: String, output: (ProtoBuf.Function.Builder) -> Unit): KmFunctionVisitor =
object : KmFunctionVisitor() {
val t = ProtoBuf.Function.newBuilder()
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor? =
writeTypeParameter(c, flags, name, id, variance) { t.addTypeParameter(it) }
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.receiverType = it.build() }
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
writeType(c, flags) { t.addContextReceiverType(it) }
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor? =
writeValueParameter(c, flags, name) { t.addValueParameter(it) }
override fun visitReturnType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.returnType = it.build() }
override fun visitVersionRequirement(): KmVersionRequirementVisitor? =
writeVersionRequirement(c) { t.addVersionRequirement(it) }
@OptIn(ExperimentalContracts::class)
override fun visitContract(): KmContractVisitor? =
writeContract(c) { t.contract = it.build() }
override fun visitExtensions(type: KmExtensionType): KmFunctionExtensionVisitor? =
applySingleExtension(type) {
writeFunctionExtensions(type, t, c)
}
override fun visitEnd() {
t.name = c[name]
if (flags != ProtoBuf.Function.getDefaultInstance().flags) {
t.flags = flags
}
output(t)
}
}
public fun writeProperty(
c: WriteContext, flags: Int, name: String, getterFlags: Int, setterFlags: Int, output: (ProtoBuf.Property.Builder) -> Unit
): KmPropertyVisitor = object : KmPropertyVisitor() {
val t = ProtoBuf.Property.newBuilder()
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor? =
writeTypeParameter(c, flags, name, id, variance) { t.addTypeParameter(it) }
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.receiverType = it.build() }
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
writeType(c, flags) { t.addContextReceiverType(it) }
override fun visitSetterParameter(flags: Int, name: String): KmValueParameterVisitor? =
writeValueParameter(c, flags, name) { t.setterValueParameter = it.build() }
override fun visitReturnType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.returnType = it.build() }
override fun visitVersionRequirement(): KmVersionRequirementVisitor? =
writeVersionRequirement(c) { t.addVersionRequirement(it) }
override fun visitExtensions(type: KmExtensionType): KmPropertyExtensionVisitor? =
applySingleExtension(type) {
writePropertyExtensions(type, t, c)
}
override fun visitEnd() {
t.name = c[name]
if (flags != ProtoBuf.Property.getDefaultInstance().flags) {
t.flags = flags
}
// TODO: do not write getterFlags/setterFlags if not needed
if (Flag.Property.HAS_GETTER(flags)) t.getterFlags = getterFlags
if (Flag.Property.HAS_SETTER(flags)) t.setterFlags = setterFlags
output(t)
}
}
private fun writeValueParameter(
c: WriteContext, flags: Int, name: String,
output: (ProtoBuf.ValueParameter.Builder) -> Unit
): KmValueParameterVisitor = object : KmValueParameterVisitor() {
val t = ProtoBuf.ValueParameter.newBuilder()
override fun visitType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.type = it.build() }
override fun visitVarargElementType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.varargElementType = it.build() }
override fun visitExtensions(type: KmExtensionType): KmValueParameterExtensionVisitor? =
applySingleExtension(type) {
writeValueParameterExtensions(type, t, c)
}
override fun visitEnd() {
if (flags != ProtoBuf.ValueParameter.getDefaultInstance().flags) {
t.flags = flags
}
t.name = c[name]
output(t)
}
}
private fun writeTypeAlias(
c: WriteContext, flags: Int, name: String,
output: (ProtoBuf.TypeAlias.Builder) -> Unit
): KmTypeAliasVisitor = object : KmTypeAliasVisitor() {
val t = ProtoBuf.TypeAlias.newBuilder()
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor? =
writeTypeParameter(c, flags, name, id, variance) { t.addTypeParameter(it) }
override fun visitUnderlyingType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.underlyingType = it.build() }
override fun visitExpandedType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.expandedType = it.build() }
override fun visitAnnotation(annotation: KmAnnotation) {
t.addAnnotation(annotation.writeAnnotation(c.strings))
}
override fun visitVersionRequirement(): KmVersionRequirementVisitor? =
writeVersionRequirement(c) { t.addVersionRequirement(it) }
override fun visitExtensions(type: KmExtensionType): KmTypeAliasExtensionVisitor? =
applySingleExtension(type) {
writeTypeAliasExtensions(type, t, c)
}
override fun visitEnd() {
if (flags != ProtoBuf.TypeAlias.getDefaultInstance().flags) {
t.flags = flags
}
t.name = c[name]
output(t)
}
}
private fun writeVersionRequirement(
c: WriteContext, output: (Int) -> Unit
): KmVersionRequirementVisitor = object : KmVersionRequirementVisitor() {
var t: ProtoBuf.VersionRequirement.Builder? = null
override fun visit(kind: KmVersionRequirementVersionKind, level: KmVersionRequirementLevel, errorCode: Int?, message: String?) {
t = ProtoBuf.VersionRequirement.newBuilder().apply {
val versionKind = when (kind) {
KmVersionRequirementVersionKind.LANGUAGE_VERSION -> ProtoBuf.VersionRequirement.VersionKind.LANGUAGE_VERSION
KmVersionRequirementVersionKind.COMPILER_VERSION -> ProtoBuf.VersionRequirement.VersionKind.COMPILER_VERSION
KmVersionRequirementVersionKind.API_VERSION -> ProtoBuf.VersionRequirement.VersionKind.API_VERSION
}
if (versionKind != defaultInstanceForType.versionKind) {
this.versionKind = versionKind
}
val requirementLevel = when (level) {
KmVersionRequirementLevel.WARNING -> ProtoBuf.VersionRequirement.Level.WARNING
KmVersionRequirementLevel.ERROR -> ProtoBuf.VersionRequirement.Level.ERROR
KmVersionRequirementLevel.HIDDEN -> ProtoBuf.VersionRequirement.Level.HIDDEN
}
if (requirementLevel != defaultInstanceForType.level) {
this.level = requirementLevel
}
if (errorCode != null) {
this.errorCode = errorCode
}
if (message != null) {
this.message = c[message]
}
}
}
override fun visitVersion(major: Int, minor: Int, patch: Int) {
if (t == null) {
throw IllegalStateException("KmVersionRequirementVisitor.visit has not been called")
}
VersionRequirement.Version(major, minor, patch).encode(
writeVersion = { t!!.version = it },
writeVersionFull = { t!!.versionFull = it }
)
}
override fun visitEnd() {
if (t == null) {
throw IllegalStateException("KmVersionRequirementVisitor.visit has not been called")
}
output(c.versionRequirements[t!!])
}
}
@ExperimentalContracts
private fun writeContract(c: WriteContext, output: (ProtoBuf.Contract.Builder) -> Unit): KmContractVisitor =
object : KmContractVisitor() {
val t = ProtoBuf.Contract.newBuilder()
override fun visitEffect(type: KmEffectType, invocationKind: KmEffectInvocationKind?): KmEffectVisitor? =
writeEffect(c, type, invocationKind) { t.addEffect(it) }
override fun visitEnd() {
output(t)
}
}
@ExperimentalContracts
private fun writeEffect(
c: WriteContext, type: KmEffectType, invocationKind: KmEffectInvocationKind?,
output: (ProtoBuf.Effect.Builder) -> Unit
): KmEffectVisitor = object : KmEffectVisitor() {
val t = ProtoBuf.Effect.newBuilder()
override fun visitConstructorArgument(): KmEffectExpressionVisitor? =
writeEffectExpression(c) { t.addEffectConstructorArgument(it) }
override fun visitConclusionOfConditionalEffect(): KmEffectExpressionVisitor? =
writeEffectExpression(c) { t.conclusionOfConditionalEffect = it.build() }
@Suppress("UNUSED_VARIABLE") // force exhaustive whens
override fun visitEnd() {
val unused = when (type) {
KmEffectType.RETURNS_CONSTANT -> t.effectType = ProtoBuf.Effect.EffectType.RETURNS_CONSTANT
KmEffectType.CALLS -> t.effectType = ProtoBuf.Effect.EffectType.CALLS
KmEffectType.RETURNS_NOT_NULL -> t.effectType = ProtoBuf.Effect.EffectType.RETURNS_NOT_NULL
}
val unused2 = when (invocationKind) {
KmEffectInvocationKind.AT_MOST_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.AT_MOST_ONCE
KmEffectInvocationKind.EXACTLY_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.EXACTLY_ONCE
KmEffectInvocationKind.AT_LEAST_ONCE -> t.kind = ProtoBuf.Effect.InvocationKind.AT_LEAST_ONCE
null -> null
}
output(t)
}
}
@ExperimentalContracts
private fun writeEffectExpression(c: WriteContext, output: (ProtoBuf.Expression.Builder) -> Unit): KmEffectExpressionVisitor =
object : KmEffectExpressionVisitor() {
val t = ProtoBuf.Expression.newBuilder()
override fun visit(flags: Int, parameterIndex: Int?) {
if (flags != ProtoBuf.Expression.getDefaultInstance().flags) {
t.flags = flags
}
if (parameterIndex != null) {
t.valueParameterReference = parameterIndex
}
}
override fun visitConstantValue(value: Any?) {
when (value) {
true -> t.constantValue = ProtoBuf.Expression.ConstantValue.TRUE
false -> t.constantValue = ProtoBuf.Expression.ConstantValue.FALSE
null -> t.constantValue = ProtoBuf.Expression.ConstantValue.NULL
else -> throw IllegalArgumentException("Only true, false or null constant values are allowed for effects (was=$value)")
}
}
override fun visitIsInstanceType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.isInstanceType = it.build() }
override fun visitAndArgument(): KmEffectExpressionVisitor? =
writeEffectExpression(c) { t.addAndArgument(it) }
override fun visitOrArgument(): KmEffectExpressionVisitor? =
writeEffectExpression(c) { t.addOrArgument(it) }
override fun visitEnd() {
output(t)
}
}
public open class ClassWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) : KmClassVisitor() {
protected val t: ProtoBuf.Class.Builder = ProtoBuf.Class.newBuilder()!!
protected val c: WriteContext = WriteContext(stringTable, contextExtensions)
override fun visit(flags: Int, name: ClassName) {
if (flags != ProtoBuf.Class.getDefaultInstance().flags) {
t.flags = flags
}
t.fqName = c.getClassName(name)
}
override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor? =
writeTypeParameter(c, flags, name, id, variance) { t.addTypeParameter(it) }
override fun visitSupertype(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.addSupertype(it) }
override fun visitConstructor(flags: Int): KmConstructorVisitor? =
writeConstructor(c, flags) { t.addConstructor(it) }
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor? =
writeFunction(c, flags, name) { t.addFunction(it) }
override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor? =
writeProperty(c, flags, name, getterFlags, setterFlags) { t.addProperty(it) }
override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor? =
writeTypeAlias(c, flags, name) { t.addTypeAlias(it) }
override fun visitCompanionObject(name: String) {
t.companionObjectName = c[name]
}
override fun visitNestedClass(name: String) {
t.addNestedClassName(c[name])
}
override fun visitEnumEntry(name: String) {
t.addEnumEntry(ProtoBuf.EnumEntry.newBuilder().also { enumEntry ->
enumEntry.name = c[name]
})
}
override fun visitSealedSubclass(name: ClassName) {
t.addSealedSubclassFqName(c.getClassName(name))
}
override fun visitInlineClassUnderlyingPropertyName(name: String) {
t.inlineClassUnderlyingPropertyName = c[name]
}
override fun visitInlineClassUnderlyingType(flags: Int): KmTypeVisitor? =
writeType(c, flags) { t.inlineClassUnderlyingType = it.build() }
@ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
writeType(c, flags) { t.addContextReceiverType(it) }
override fun visitVersionRequirement(): KmVersionRequirementVisitor? =
writeVersionRequirement(c) { t.addVersionRequirement(it) }
override fun visitExtensions(type: KmExtensionType): KmClassExtensionVisitor? =
applySingleExtension(type) {
writeClassExtensions(type, t, c)
}
override fun visitEnd() {
c.versionRequirements.serialize()?.let {
t.versionRequirementTable = it
}
}
}
public open class PackageWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) : KmPackageVisitor() {
protected val t: ProtoBuf.Package.Builder = ProtoBuf.Package.newBuilder()!!
protected val c: WriteContext = WriteContext(stringTable, contextExtensions)
public open class PackageWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) {
public val t: ProtoBuf.Package.Builder = ProtoBuf.Package.newBuilder()
public val c: WriteContext = WriteContext(stringTable, contextExtensions)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor? =
writeFunction(c, flags, name) { t.addFunction(it) }
override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor? =
writeProperty(c, flags, name, getterFlags, setterFlags) { t.addProperty(it) }
override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor? =
writeTypeAlias(c, flags, name) { t.addTypeAlias(it) }
override fun visitExtensions(type: KmExtensionType): KmPackageExtensionVisitor? =
applySingleExtension(type) {
writePackageExtensions(type, t, c)
}
override fun visitEnd() {
public fun writePackage(kmPackage: KmPackage) {
t.addAllFunction(kmPackage.functions.map { c.writeFunction(it).build() })
t.addAllProperty(kmPackage.properties.map { c.writeProperty(it).build() })
t.addAllTypeAlias(kmPackage.typeAliases.map { c.writeTypeAlias(it).build() })
kmPackage.extensions.matchExtensions { writePackageExtensions(it, t, c) }
c.versionRequirements.serialize()?.let {
t.versionRequirementTable = it
}
}
}
public open class ModuleFragmentWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) :
KmModuleFragmentVisitor() {
public open class ModuleFragmentWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) {
protected val t: ProtoBuf.PackageFragment.Builder = ProtoBuf.PackageFragment.newBuilder()!!
protected val c: WriteContext = WriteContext(stringTable, contextExtensions)
override fun visitPackage(): KmPackageVisitor? = object : PackageWriter(c.strings, c.contextExtensions) {
override fun visitEnd() {
super.visitEnd()
this@ModuleFragmentWriter.t.setPackage(t)
public open fun writeModuleFragment(kmPackageFragment: KmModuleFragment) {
kmPackageFragment.pkg?.let {
val pkgWriter = PackageWriter(c.strings, c.contextExtensions)
pkgWriter.writePackage(it)
t.setPackage(pkgWriter.t)
}
}
override fun visitClass(): KmClassVisitor? = object : ClassWriter(c.strings, c.contextExtensions) {
override fun visitEnd() {
super.visitEnd()
this@ModuleFragmentWriter.t.addClass_(t)
kmPackageFragment.classes.forEach {
val classWriter = ClassWriter(c.strings, c.contextExtensions)
classWriter.writeClass(it)
t.addClass_(classWriter.t)
}
}
override fun visitExtensions(type: KmExtensionType): KmModuleFragmentExtensionVisitor? =
applySingleExtension(type) {
writeModuleFragmentExtensions(type, t, c)
}
kmPackageFragment.extensions.writeExtensions { writeModuleFragmentExtensions(it, t, c) }
}
}
public open class LambdaWriter(stringTable: StringTable) : KmLambdaVisitor() {
protected var t: ProtoBuf.Function.Builder? = null
protected val c: WriteContext = WriteContext(stringTable)
public open class LambdaWriter(stringTable: StringTable) {
public var t: ProtoBuf.Function.Builder? = null
public val c: WriteContext = WriteContext(stringTable)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor? =
writeFunction(c, flags, name) { t = it }
public fun writeLambda(kmLambda: KmLambda) {
t = c.writeFunction(kmLambda.function)
}
}
@@ -2,13 +2,14 @@
* Copyright 2010-2022 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.internal.common
import kotlinx.metadata.*
import kotlinx.metadata.VISITOR_API_MESSAGE
import kotlinx.metadata.internal.accept
import kotlinx.metadata.internal.toKmPackage
import kotlinx.metadata.internal.toKmClass
import kotlinx.metadata.internal.extensions.KmModuleFragmentExtension
import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.extensions.singleOfType
@@ -23,23 +24,33 @@ import java.io.ByteArrayInputStream
* distribution can also be read with this reader.
*/
public class KotlinCommonMetadata private constructor(private val proto: ProtoBuf.PackageFragment) {
public val kmModuleFragment: KmModuleFragment = readImpl()
@Deprecated(
"To avoid excessive copying, use .kmModuleFragment property instead. Note that it returns a view and not a copy.",
ReplaceWith("kmModuleFragment"),
DeprecationLevel.WARNING
)
public fun toKmModuleFragment(): KmModuleFragment =
KmModuleFragment().apply(this::accept)
KmModuleFragment().apply { kmModuleFragment.accept(this) }
// private because there are no use-cases and it is not finished
private class Writer : KmModuleFragmentVisitor() {
// TODO
}
public fun accept(v: KmModuleFragmentVisitor) {
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmModuleFragmentVisitor): Unit = kmModuleFragment.accept(v)
private fun readImpl(): KmModuleFragment {
val v = KmModuleFragment()
val strings = NameResolverImpl(proto.strings, proto.qualifiedNames)
if (proto.hasPackage()) {
v.visitPackage()?.let { proto.`package`.accept(it, strings) }
v.pkg = proto.`package`.toKmPackage(strings)
}
for (klass in proto.class_List) {
v.visitClass()?.let { klass.accept(it, strings) }
}
v.visitEnd()
proto.class_List.mapTo(v.classes) { it.toKmClass(strings) }
return v
}
public companion object {
@@ -62,7 +73,6 @@ public class KotlinCommonMetadata private constructor(private val proto: ProtoBu
*
* Can be read with [KotlinCommonMetadata.read].
*/
@Suppress("DEPRECATION")
public class KmModuleFragment : KmModuleFragmentVisitor() {
/**
@@ -75,18 +85,18 @@ public class KmModuleFragment : KmModuleFragmentVisitor() {
*/
public val classes: MutableList<KmClass> = ArrayList()
private val extensions: List<KmModuleFragmentExtension> =
internal val extensions: List<KmModuleFragmentExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createModuleFragmentExtensions)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitPackage(): KmPackageVisitor? =
KmPackage().also { pkg = it }
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmModuleFragmentExtensionVisitor? =
extensions.singleOfType(type)
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitClass(): KmClassVisitor? =
KmClass().addTo(classes)
@@ -95,7 +105,7 @@ public class KmModuleFragment : KmModuleFragmentVisitor() {
*
* @param visitor the visitor which will visit data in the module fragment.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmModuleFragmentVisitor) {
pkg?.let { visitor.visitPackage()?.let(it::accept) }
classes.forEach { visitor.visitClass()?.let(it::accept) }
@@ -110,8 +120,7 @@ public class KmModuleFragment : KmModuleFragmentVisitor() {
*
* When using this class, [visitEnd] must be called exactly once and after calls to all other visit* methods.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Suppress("DEPRECATION")
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public abstract class KmModuleFragmentVisitor @JvmOverloads constructor(private val delegate: KmModuleFragmentVisitor? = null) {
/**
@@ -145,5 +154,5 @@ public abstract class KmModuleFragmentVisitor @JvmOverloads constructor(private
/**
* A visitor to visit platform-specific extensions for a module fragment.
*/
@Deprecated(VISITOR_API_MESSAGE)
@Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmModuleFragmentExtensionVisitor : KmExtensionVisitor
@@ -2,7 +2,7 @@
* Copyright 2010-2022 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.internal.extensions
@@ -2,7 +2,7 @@
* Copyright 2010-2022 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.
*/
@file:Suppress("DEPRECATION")
@file:Suppress("DEPRECATION_ERROR")
package kotlinx.metadata.internal.extensions
import kotlinx.metadata.KmExtensionType
@@ -20,6 +20,17 @@ internal fun <T : KmExtensionVisitor> applySingleExtension(type: KmExtensionType
return result
}
internal fun <E : KmExtension<*>> List<E>.matchExtensions(block: MetadataExtensions.(E) -> Boolean) {
forEach { ext ->
var processed = false
for (extesion in MetadataExtensions.INSTANCES) {
val current = extesion.block(ext)
if (processed && current) throw IllegalStateException("Multiple extensions handle the same extension type: ${ext.type}")
processed = current
}
}
}
internal fun <N : KmExtension<*>> Collection<N>.singleOfType(type: KmExtensionType): N {
var result: N? = null
for (node in this) {
@@ -11,7 +11,7 @@ import kotlinx.metadata.internal.common.*
import org.jetbrains.kotlin.metadata.ProtoBuf
import java.util.*
@Suppress("DEPRECATION")
@Suppress("DEPRECATION_ERROR")
public interface MetadataExtensions {
public fun readClassExtensions(v: KmClassVisitor, proto: ProtoBuf.Class, c: ReadContext)
@@ -33,9 +33,9 @@ public interface MetadataExtensions {
public fun readValueParameterExtensions(v: KmValueParameterVisitor, proto: ProtoBuf.ValueParameter, c: ReadContext)
public fun writeClassExtensions(type: KmExtensionType, proto: ProtoBuf.Class.Builder, c: WriteContext): KmClassExtensionVisitor?
public fun writeClassExtensions(extension: KmClassExtension, proto: ProtoBuf.Class.Builder, c: WriteContext): Boolean
public fun writePackageExtensions(type: KmExtensionType, proto: ProtoBuf.Package.Builder, c: WriteContext): KmPackageExtensionVisitor?
public fun writePackageExtensions(extension: KmPackageExtension, proto: ProtoBuf.Package.Builder, c: WriteContext): Boolean
public fun writeModuleFragmentExtensions(
type: KmExtensionType, proto: ProtoBuf.PackageFragment.Builder, c: WriteContext
@@ -183,7 +183,7 @@ private fun printType(type: KmType): String {
val abbreviatedType = type.abbreviatedType?.let(::printType)
val outerType = type.outerType?.let(::printType)
val platformTypeUpperBound = type.flexibleTypeUpperBound?.let {
@Suppress("DEPRECATION")
@Suppress("DEPRECATION_ERROR")
(if (it.typeFlexibilityId == JvmTypeExtensionVisitor.PLATFORM_TYPE_ID) {
printType(it.type)
} else null)
@@ -60,7 +60,6 @@ private fun compileAndPrintAllFiles(
useK2: Boolean,
) {
val main = StringBuilder()
val afterVisitors = StringBuilder()
val afterNodes = StringBuilder()
val kotlinp = Kotlinp(KotlinpSettings(isVerbose = true, sortDeclarations = true))
@@ -70,11 +69,10 @@ private fun compileAndPrintAllFiles(
when (outputFile.extension) {
"kotlin_module" -> {
val moduleFile = kotlinp.readModuleFile(outputFile)!!
val transformedWithVisitors = KotlinModuleMetadata.read(transformModuleFileWithReadWriteVisitors(moduleFile))
val transformedWithNodes = KotlinModuleMetadata.read(transformModuleFileWithNodes(moduleFile))
for ((sb, moduleFileToRender) in listOf(
main to moduleFile, afterVisitors to transformedWithVisitors, afterNodes to transformedWithNodes
main to moduleFile, afterNodes to transformedWithNodes
)) {
sb.appendFileName(outputFile.relativeTo(tmpdir))
sb.append(kotlinp.renderModuleFile(moduleFileToRender))
@@ -83,11 +81,10 @@ private fun compileAndPrintAllFiles(
"class" -> {
val metadata = kotlinp.readClassFile(outputFile)
val classFile = kotlinp.readMetadata(metadata)
val classFile2 = transformClassFileWithReadWriteVisitors(classFile)
val classFile3 = KotlinClassMetadata.read(transformClassFileWithNodes(metadata, classFile))
for ((sb, classFileToRender) in listOf(
main to classFile, afterVisitors to classFile2, afterNodes to classFile3
main to classFile, afterNodes to classFile3
)) {
sb.appendFileName(outputFile.relativeTo(tmpdir))
sb.append(kotlinp.renderClassFile(classFileToRender))
@@ -102,7 +99,6 @@ private fun compileAndPrintAllFiles(
}
if (readWriteAndCompare && InTextDirectivesUtils.findStringWithPrefixes(file.readText(), "// NO_READ_WRITE_COMPARE") == null) {
assertEquals("Metadata is different after transformation with visitors.", main.toString(), afterVisitors.toString())
assertEquals("Metadata is different after transformation with nodes.", main.toString(), afterNodes.toString())
}
}
@@ -130,27 +126,6 @@ private fun StringBuilder.appendFileName(file: File) {
appendLine("// ------------------------------------------")
}
// Reads the class file and writes it back with *Writer visitors.
// The resulting class file should be the same from the point of view of any metadata reader, including kotlinp
// (the exact bytes may differ though, because there are multiple ways to encode the same metadata)
@Suppress("DEPRECATION")
private fun transformClassFileWithReadWriteVisitors(classFile: KotlinClassMetadata): KotlinClassMetadata =
when (classFile) {
is KotlinClassMetadata.Class -> KotlinClassMetadata.Class.Writer().apply(classFile::accept).write()
is KotlinClassMetadata.FileFacade -> KotlinClassMetadata.FileFacade.Writer().apply(classFile::accept).write()
is KotlinClassMetadata.SyntheticClass -> {
val writer = KotlinClassMetadata.SyntheticClass.Writer()
if (classFile.isLambda) {
classFile.accept(writer)
}
writer.write()
}
is KotlinClassMetadata.MultiFileClassFacade -> KotlinClassMetadata.MultiFileClassFacade.Writer().write(classFile.partClassNames)
is KotlinClassMetadata.MultiFileClassPart ->
KotlinClassMetadata.MultiFileClassPart.Writer().apply(classFile::accept).write(classFile.facadeClassName)
else -> classFile
}
// Reads the class file and writes it back with KmClass/KmFunction/... elements.
private fun transformClassFileWithNodes(metadata: Metadata, classFile: KotlinClassMetadata): Metadata =
when (classFile) {
@@ -166,11 +141,6 @@ private fun transformClassFileWithNodes(metadata: Metadata, classFile: KotlinCla
is KotlinClassMetadata.Unknown -> metadata
}
@Suppress("DEPRECATION") // We're testing that reading/writing with KmNodes is identical to direct
@OptIn(UnstableMetadataApi::class)
private fun transformModuleFileWithReadWriteVisitors(moduleFile: KotlinModuleMetadata): ByteArray =
KotlinModuleMetadata.Writer().apply(moduleFile::accept).write()
@OptIn(UnstableMetadataApi::class)
private fun transformModuleFileWithNodes(moduleFile: KotlinModuleMetadata): ByteArray =
KotlinModuleMetadata.write(moduleFile.kmModule)