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 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 <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 final fun write ([I)[B
public static synthetic fun write$default (Lkotlinx/metadata/jvm/KotlinModuleMetadata$Writer;[IILjava/lang/Object;)[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. * 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. * 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 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. * 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( public abstract class JvmDeclarationContainerExtensionVisitor @JvmOverloads constructor(
protected open val delegate: JvmDeclarationContainerExtensionVisitor? = null protected open val delegate: JvmDeclarationContainerExtensionVisitor? = null
) : KmDeclarationContainerExtensionVisitor { ) : KmDeclarationContainerExtensionVisitor {
@@ -46,7 +46,7 @@ public abstract class JvmDeclarationContainerExtensionVisitor @JvmOverloads cons
/** /**
* A visitor to visit JVM extensions for a class. * 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( public open class JvmClassExtensionVisitor @JvmOverloads constructor(
delegate: JvmClassExtensionVisitor? = null delegate: JvmClassExtensionVisitor? = null
) : KmClassExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) { ) : KmClassExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) {
@@ -92,7 +92,7 @@ public open class JvmClassExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a package fragment. * 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( public open class JvmPackageExtensionVisitor @JvmOverloads constructor(
delegate: JvmPackageExtensionVisitor? = null delegate: JvmPackageExtensionVisitor? = null
) : KmPackageExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) { ) : KmPackageExtensionVisitor, JvmDeclarationContainerExtensionVisitor(delegate) {
@@ -123,7 +123,7 @@ public open class JvmPackageExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a function. * 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( public open class JvmFunctionExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmFunctionExtensionVisitor? = null private val delegate: JvmFunctionExtensionVisitor? = null
) : KmFunctionExtensionVisitor { ) : KmFunctionExtensionVisitor {
@@ -170,7 +170,7 @@ public open class JvmFunctionExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a property. * 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( public open class JvmPropertyExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmPropertyExtensionVisitor? = null private val delegate: JvmPropertyExtensionVisitor? = null
) : KmPropertyExtensionVisitor { ) : KmPropertyExtensionVisitor {
@@ -259,7 +259,7 @@ public open class JvmPropertyExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a 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( public open class JvmConstructorExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmConstructorExtensionVisitor? = null private val delegate: JvmConstructorExtensionVisitor? = null
) : KmConstructorExtensionVisitor { ) : KmConstructorExtensionVisitor {
@@ -291,7 +291,7 @@ public open class JvmConstructorExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a type parameter. * 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( public open class JvmTypeParameterExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmTypeParameterExtensionVisitor? = null private val delegate: JvmTypeParameterExtensionVisitor? = null
) : KmTypeParameterExtensionVisitor { ) : KmTypeParameterExtensionVisitor {
@@ -328,7 +328,7 @@ public open class JvmTypeParameterExtensionVisitor @JvmOverloads constructor(
/** /**
* A visitor to visit JVM extensions for a type. * 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( public open class JvmTypeExtensionVisitor @JvmOverloads constructor(
private val delegate: JvmTypeExtensionVisitor? = null private val delegate: JvmTypeExtensionVisitor? = null
) : KmTypeExtensionVisitor { ) : KmTypeExtensionVisitor {
@@ -2,7 +2,7 @@
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.jvm
@@ -58,9 +58,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* *
* Returns the same (mutable) [KmClass] instance every time. * 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) 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 * @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) public fun accept(v: KmClassVisitor): Unit = kmClass.accept(v)
/** /**
@@ -86,6 +86,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/ */
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
public class Writer : ClassWriter(JvmStringTable()) { public class Writer : ClassWriter(JvmStringTable()) {
/** /**
@@ -99,15 +100,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@JvmOverloads @JvmOverloads
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeClass(kmClass, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
public fun write( public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0 extraInt: Int = 0
): Class { ): Class {
checkMetadataVersion(metadataVersion) error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeClass.")
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(CLASS_KIND, metadataVersion, d1, d2, extraInt = extraInt)
return Class(metadata)
} }
} }
} }
@@ -127,9 +126,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* *
* Returns the same (mutable) [KmPackage] instance every time. * 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) 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 * @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) public fun accept(v: KmPackageVisitor): Unit = kmPackage.accept(v)
/** /**
@@ -155,6 +154,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/ */
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
public class Writer : PackageWriter(JvmStringTable()) { public class Writer : PackageWriter(JvmStringTable()) {
/** /**
@@ -168,15 +168,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@JvmOverloads @JvmOverloads
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeFileFacade(kmPackage, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
public fun write( public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0 extraInt: Int = 0
): FileFacade { ): FileFacade {
checkMetadataVersion(metadataVersion) error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeFileFacade.")
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(FILE_FACADE_KIND, metadataVersion, d1, d2, extraInt = extraInt)
return FileFacade(metadata)
} }
} }
} }
@@ -203,9 +201,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* *
* Returns the same (mutable) [KmLambda] instance every time. * 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!! 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 * @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) { public fun accept(v: KmLambdaVisitor) {
if (!isLambda) throw IllegalArgumentException( if (!isLambda) throw IllegalArgumentException(
"accept(KmLambdaVisitor) is only possible for synthetic classes which are lambdas (isLambda = true)" "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( @Deprecated(
WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " + WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " +
"or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class", "or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class",
level = DeprecationLevel.ERROR
) )
public class Writer : LambdaWriter(JvmStringTable()) { public class Writer : LambdaWriter(JvmStringTable()) {
/** /**
@@ -257,19 +256,14 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
@Deprecated( @Deprecated(
WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " + WRITER_API_MESSAGE + ": KotlinClassMetadata.writeLambda(kmLambda, metadataVersion, extraInt) " +
"or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class", "or KotlinClassMetadata.writeSyntheticClass(metadataVersion, extraInt) for a non-lambda synthetic class",
level = DeprecationLevel.ERROR
) )
@JvmOverloads @JvmOverloads
public fun write( public fun write(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0 extraInt: Int = 0
): SyntheticClass { ): SyntheticClass {
checkMetadataVersion(metadataVersion) error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeLambda or KotlinClassMetadata.writeSyntheticClass.")
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)
} }
} }
} }
@@ -316,6 +310,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/ */
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
public class Writer { public class Writer {
/** /**
@@ -329,6 +324,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
*/ */
@Deprecated( @Deprecated(
"$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)", "$WRITER_API_MESSAGE, such as KotlinClassMetadata.writeMultiFileClassFacade(partClassNames, metadataVersion, extraInt)",
level = DeprecationLevel.ERROR
) )
@JvmOverloads @JvmOverloads
public fun write( public fun write(
@@ -336,12 +332,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0 extraInt: Int = 0
): MultiFileClassFacade { ): MultiFileClassFacade {
checkMetadataVersion(metadataVersion) error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeMultiFileClassFacade.")
val metadata = Metadata(
MULTI_FILE_CLASS_FACADE_KIND, metadataVersion,
partClassNames.toTypedArray(), extraInt = extraInt
)
return MultiFileClassFacade(metadata)
} }
} }
} }
@@ -363,9 +354,9 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* *
* Returns the same (mutable) [KmPackage] instance every time. * 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) 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 * @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) { public fun accept(v: KmPackageVisitor) {
kmPackage.accept(v) 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. * A [KmPackageVisitor] that generates the metadata of a multi-file class part.
*/ */
@Deprecated( @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()) { public class Writer : PackageWriter(JvmStringTable()) {
/** /**
@@ -411,7 +403,8 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
* 0 by default * 0 by default
*/ */
@Deprecated( @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 @JvmOverloads
public fun write( public fun write(
@@ -419,12 +412,7 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0 extraInt: Int = 0
): MultiFileClassPart { ): MultiFileClassPart {
checkMetadataVersion(metadataVersion) error("This method is no longer implemented. Migrate to KotlinClassMetadata.writeMultifileClassPart.")
val (d1, d2) = writeProtoBufData(t.build(), c)
val metadata = Metadata(
MULTI_FILE_CLASS_PART_KIND, metadataVersion, d1, d2, facadeClassName, extraInt = extraInt
)
return MultiFileClassPart(metadata)
} }
} }
} }
@@ -457,7 +445,11 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE { ): 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, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE { ): 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, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE { ): 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( public fun writeSyntheticClass(
metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, 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. * Writes metadata of the multi-file class facade.
@@ -533,7 +539,13 @@ public sealed class KotlinClassMetadata(internal val annotationData: Metadata) {
public fun writeMultiFileClassFacade( public fun writeMultiFileClassFacade(
partClassNames: List<String>, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION, partClassNames: List<String>, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, 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. * 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, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION,
extraInt: Int = 0, extraInt: Int = 0,
): Metadata = wrapWriteIntoIAE { ): 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( @file:Suppress(
"MemberVisibilityCanBePrivate", "MemberVisibilityCanBePrivate",
"DEPRECATION", "DEPRECATION",
"DEPRECATION_ERROR",
"INVISIBLE_MEMBER", // InconsistentKotlinMetadataException "INVISIBLE_MEMBER", // InconsistentKotlinMetadataException
"INVISIBLE_REFERENCE" "INVISIBLE_REFERENCE",
"UNUSED_PARAMETER"
) )
package kotlinx.metadata.jvm package kotlinx.metadata.jvm
import kotlinx.metadata.* import kotlinx.metadata.*
import kotlinx.metadata.internal.accept import kotlinx.metadata.internal.toKmClass
import kotlinx.metadata.jvm.internal.IgnoreInApiDump
import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.COMPATIBLE_METADATA_VERSION import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.COMPATIBLE_METADATA_VERSION
import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.throwIfNotCompatible import kotlinx.metadata.jvm.KotlinClassMetadata.Companion.throwIfNotCompatible
import kotlinx.metadata.jvm.internal.wrapIntoMetadataExceptionWhenNeeded import kotlinx.metadata.jvm.internal.wrapIntoMetadataExceptionWhenNeeded
@@ -47,7 +48,7 @@ public class KotlinModuleMetadata private constructor(
* *
* Returns the same (mutable) [KmModule] instance every time. * 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. * 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"), ReplaceWith("kmModule"),
DeprecationLevel.WARNING 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. * 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)") @Deprecated(
public class Writer : KmModuleVisitor() { "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() private val b = JvmModuleProtoBuf.Module.newBuilder()
override fun visitPackageParts(fqName: String, fileFacades: List<String>, multiFileClassParts: Map<String, String>) { private fun writeModule(kmModule: KmModule) {
PackageParts(fqName).apply { kmModule.packageParts.forEach { (fqName, packageParts) ->
for (fileFacade in fileFacades) { PackageParts(fqName).apply {
addPart(fileFacade, null) for (fileFacade in packageParts.fileFacades) {
} addPart(fileFacade, null)
for ((multiFileClassPart, multiFileFacade) in multiFileClassParts) { }
addPart(multiFileClassPart, multiFileFacade) 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 // TODO: move StringTableImpl to module 'metadata' and support module annotations here
b.addAnnotation(ProtoBuf.Annotation.newBuilder().apply { b.addAnnotation(ProtoBuf.Annotation.newBuilder().apply {
id = annotation.className.name // <-- use StringTableImpl here id = annotation.className.name // <-- use StringTableImpl here
}) })
*/ */
}
override fun visitOptionalAnnotationClass(): KmClassVisitor? { // visitOptionalAnnotationClass
/* /*
return object : ClassWriter(TODO() /* use StringTableImpl here */) { return object : ClassWriter(TODO() /* use StringTableImpl here */) {
override fun visitEnd() { 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]), * @param metadataVersion metadata version to be written to the metadata (see [Metadata.metadataVersion]),
* [KotlinClassMetadata.COMPATIBLE_METADATA_VERSION] by default * [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 { 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 * @param v the visitor that must visit this module file
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(v: KmModuleVisitor) { public fun accept(v: KmModuleVisitor): Unit = kmModule.accept(v)
private fun readImpl(): KmModule {
val v = KmModule()
for ((fqName, parts) in data.packageFqName2Parts) { for ((fqName, parts) in data.packageFqName2Parts) {
val (fileFacades, multiFileClassParts) = parts.parts.partition { parts.getMultifileFacadeName(it) == null } 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) { for (annotation in data.moduleData.annotations) {
v.visitAnnotation(KmAnnotation(annotation, emptyMap())) v.annotations.add(KmAnnotation(annotation, emptyMap()))
} }
for (classProto in data.moduleData.optionalAnnotations) { for (classProto in data.moduleData.optionalAnnotations) {
v.visitOptionalAnnotationClass()?.let { v.optionalAnnotationClasses.add(classProto.toKmClass(data.moduleData.nameResolver))
classProto.accept(it, data.moduleData.nameResolver)
}
} }
return v
v.visitEnd()
} }
/** /**
@@ -173,7 +182,8 @@ public class KotlinModuleMetadata private constructor(
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
public fun write(kmModule: KmModule, metadataVersion: IntArray = COMPATIBLE_METADATA_VERSION): ByteArray = wrapWriteIntoIAE { 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 { 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. * 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 @UnstableMetadataApi
public abstract class KmModuleVisitor(private val delegate: KmModuleVisitor? = null) { public abstract class KmModuleVisitor(private val delegate: KmModuleVisitor? = null) {
/** /**
@@ -265,17 +275,17 @@ public class KmModule : KmModuleVisitor() {
*/ */
public val optionalAnnotationClasses: MutableList<KmClass> = ArrayList(0) 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>) { override fun visitPackageParts(fqName: String, fileFacades: List<String>, multiFileClassParts: Map<String, String>) {
packageParts[fqName] = KmPackageParts(fileFacades.toMutableList(), multiFileClassParts.toMutableMap()) packageParts[fqName] = KmPackageParts(fileFacades.toMutableList(), multiFileClassParts.toMutableMap())
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAnnotation(annotation: KmAnnotation) { override fun visitAnnotation(annotation: KmAnnotation) {
annotations.add(annotation) annotations.add(annotation)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOptionalAnnotationClass(): KmClass = override fun visitOptionalAnnotationClass(): KmClass =
KmClass().also(optionalAnnotationClasses::add) KmClass().also(optionalAnnotationClasses::add)
@@ -284,7 +294,7 @@ public class KmModule : KmModuleVisitor() {
* *
* @param visitor the visitor which will visit data in this module. * @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) { public fun accept(visitor: KmModuleVisitor) {
for ((fqName, parts) in packageParts) { for ((fqName, parts) in packageParts) {
visitor.visitPackageParts(fqName, parts.fileFacades, parts.multiFileClassParts) visitor.visitPackageParts(fqName, parts.fileFacades, parts.multiFileClassParts)
@@ -2,7 +2,7 @@
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 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.JvmProtoBuf
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
internal class JvmMetadataExtensions : MetadataExtensions { internal class JvmMetadataExtensions : MetadataExtensions {
override fun readClassExtensions(v: KmClassVisitor, proto: ProtoBuf.Class, c: ReadContext) { override fun readClassExtensions(v: KmClassVisitor, proto: ProtoBuf.Class, c: ReadContext) {
val ext = v.visitExtensions(JvmClassExtensionVisitor.TYPE) as? JvmClassExtensionVisitor ?: return val ext = v.visitExtensions(JvmClassExtensionVisitor.TYPE) as? JvmClassExtensionVisitor ?: return
ext as JvmClassExtension
val anonymousObjectOriginName = proto.getExtensionOrNull(JvmProtoBuf.anonymousObjectOriginName) val anonymousObjectOriginName = proto.getExtensionOrNull(JvmProtoBuf.anonymousObjectOriginName)
if (anonymousObjectOriginName != null) { if (anonymousObjectOriginName != null) {
@@ -26,9 +27,7 @@ internal class JvmMetadataExtensions : MetadataExtensions {
} }
for (property in proto.getExtension(JvmProtoBuf.classLocalVariable)) { for (property in proto.getExtension(JvmProtoBuf.classLocalVariable)) {
ext.visitLocalDelegatedProperty( ext.localDelegatedProperties.add(property.toKmProperty(c))
property.flags, c[property.name], property.getPropertyGetterFlags(), property.getPropertySetterFlags()
)?.let { property.accept(it, c) }
} }
ext.visitModuleName(proto.getExtensionOrNull(JvmProtoBuf.classModuleName)?.let(c::get) ?: JvmProtoBufUtil.DEFAULT_MODULE_NAME) 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) { override fun readPackageExtensions(v: KmPackageVisitor, proto: ProtoBuf.Package, c: ReadContext) {
val ext = v.visitExtensions(JvmPackageExtensionVisitor.TYPE) as? JvmPackageExtensionVisitor ?: return val ext = v.visitExtensions(JvmPackageExtensionVisitor.TYPE) as? JvmPackageExtensionVisitor ?: return
ext as JvmPackageExtension
for (property in proto.getExtension(JvmProtoBuf.packageLocalVariable)) { for (property in proto.getExtension(JvmProtoBuf.packageLocalVariable)) {
ext.visitLocalDelegatedProperty( ext.localDelegatedProperties.add(property.toKmProperty(c))
property.flags, c[property.name], property.getPropertyGetterFlags(), property.getPropertySetterFlags()
)?.let { property.accept(it, c) }
} }
ext.visitModuleName(proto.getExtensionOrNull(JvmProtoBuf.packageModuleName)?.let(c::get) ?: JvmProtoBufUtil.DEFAULT_MODULE_NAME) 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 readValueParameterExtensions(v: KmValueParameterVisitor, proto: ProtoBuf.ValueParameter, c: ReadContext) {}
override fun writeClassExtensions(type: KmExtensionType, proto: ProtoBuf.Class.Builder, c: WriteContext): KmClassExtensionVisitor? { override fun writeClassExtensions(extension: KmClassExtension, proto: ProtoBuf.Class.Builder, c: WriteContext): Boolean {
if (type != JvmClassExtensionVisitor.TYPE) return null if (extension.type != JvmClassExtensionVisitor.TYPE) return false
return object : JvmClassExtensionVisitor() { extension as JvmClassExtension
override fun visitAnonymousObjectOriginName(internalName: String) { extension.anonymousObjectOriginName?.let { proto.setExtension(JvmProtoBuf.anonymousObjectOriginName, c[it]) }
proto.setExtension(JvmProtoBuf.anonymousObjectOriginName, c[internalName]) extension.localDelegatedProperties.forEach {
} proto.addExtension(JvmProtoBuf.classLocalVariable, c.writeProperty(it).build())
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)
}
}
} }
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( override fun writePackageExtensions(
type: KmExtensionType, proto: ProtoBuf.Package.Builder, c: WriteContext extension: KmPackageExtension, proto: ProtoBuf.Package.Builder, c: WriteContext
): KmPackageExtensionVisitor? { ): Boolean {
if (type != JvmPackageExtensionVisitor.TYPE) return null if (extension.type != JvmPackageExtensionVisitor.TYPE) return false
return object : JvmPackageExtensionVisitor() { extension as JvmPackageExtension
override fun visitLocalDelegatedProperty( extension.localDelegatedProperties.forEach {
flags: Int, name: String, getterFlags: Int, setterFlags: Int proto.addExtension(JvmProtoBuf.packageLocalVariable, c.writeProperty(it).build())
): KmPropertyVisitor = writeProperty(c, flags, name, getterFlags, setterFlags) { }
proto.addExtension(JvmProtoBuf.packageLocalVariable, it.build()) extension.moduleName?.let { name ->
} if (name != JvmProtoBufUtil.DEFAULT_MODULE_NAME) {
proto.setExtension(JvmProtoBuf.packageModuleName, c[name])
override fun visitModuleName(name: String) {
if (name != JvmProtoBufUtil.DEFAULT_MODULE_NAME) {
proto.setExtension(JvmProtoBuf.packageModuleName, c[name])
}
} }
} }
return true
} }
// PackageFragment is not used by JVM backend. // PackageFragment is not used by JVM backend.
@@ -124,27 +124,15 @@ class MetadataSmokeTest {
@Test @Test
fun jvmInternalName() { fun jvmInternalName() {
class ClassNameReader : KmClassVisitor() {
lateinit var className: ClassName
override fun visit(flags: Int, name: ClassName) {
className = name
}
}
class L class L
val l = ClassNameReader().run { val l = (KotlinClassMetadata.read(L::class.java.getMetadata()) as KotlinClassMetadata.Class).kmClass.name
(KotlinClassMetadata.read(L::class.java.getMetadata()) as KotlinClassMetadata.Class).accept(this)
className
}
assertEquals(".kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l) assertEquals(".kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l)
assertEquals("kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l.toJvmInternalName()) assertEquals("kotlinx/metadata/test/MetadataSmokeTest\$jvmInternalName\$L", l.toJvmInternalName())
val coroutineContextKey = ClassNameReader().run { val coroutineContextKey = (KotlinClassMetadata.read(CoroutineContext.Key::class.java.getMetadata()) as KotlinClassMetadata.Class).kmClass.name
(KotlinClassMetadata.read(CoroutineContext.Key::class.java.getMetadata()) as KotlinClassMetadata.Class).accept(this)
className
}
assertEquals("kotlin/coroutines/CoroutineContext.Key", coroutineContextKey) assertEquals("kotlin/coroutines/CoroutineContext.Key", coroutineContextKey)
assertEquals("kotlin/coroutines/CoroutineContext\$Key", coroutineContextKey.toJvmInternalName()) assertEquals("kotlin/coroutines/CoroutineContext\$Key", coroutineContextKey.toJvmInternalName())
} }
@@ -154,7 +142,7 @@ class MetadataSmokeTest {
val x: suspend Int.(String, String) -> Unit = { _, _ -> } val x: suspend Int.(String, String) -> Unit = { _, _ -> }
val annotation = x::class.java.getMetadata() val annotation = x::class.java.getMetadata()
val metadata = KotlinClassMetadata.read(annotation) as KotlinClassMetadata.SyntheticClass val metadata = KotlinClassMetadata.read(annotation) as KotlinClassMetadata.SyntheticClass
metadata.accept(KmLambda()) assertNotNull(metadata.kmLambda)
} }
@Test @Test
@@ -172,19 +160,10 @@ class MetadataSmokeTest {
classWithStableParameterNames.constructors.forEach { assertFalse(it.hasNonStableParameterNames) } classWithStableParameterNames.constructors.forEach { assertFalse(it.hasNonStableParameterNames) }
classWithStableParameterNames.functions.forEach { assertFalse(it.hasNonStableParameterNames) } classWithStableParameterNames.functions.forEach { assertFalse(it.hasNonStableParameterNames) }
val newMetadata = KotlinClassMetadata.writeClass( classWithStableParameterNames.constructors.forEach { it.hasNonStableParameterNames = true }
KmClass().apply { classWithStableParameterNames.functions.forEach { it.hasNonStableParameterNames = true }
classWithStableParameterNames.accept(
object : KmClassVisitor(this) {
override fun visitConstructor(flags: Int) =
super.visitConstructor(flags + flagsOf(Flag.Constructor.HAS_NON_STABLE_PARAMETER_NAMES))
override fun visitFunction(flags: Int, name: String) = val newMetadata = KotlinClassMetadata.writeClass(classWithStableParameterNames)
super.visitFunction(flags + flagsOf(Flag.Function.HAS_NON_STABLE_PARAMETER_NAMES), name)
}
)
}
)
val classWithUnstableParameterNames = newMetadata.readAsKmClass() val classWithUnstableParameterNames = newMetadata.readAsKmClass()
@@ -2,7 +2,7 @@
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.klib
@@ -90,7 +90,7 @@ class KlibModuleMetadata(
library.packageMetadataParts(packageFqName).map { part -> library.packageMetadataParts(packageFqName).map { part ->
val packageFragment = parsePackageFragment(library.packageMetadata(packageFqName, part)) val packageFragment = parsePackageFragment(library.packageMetadata(packageFqName, part))
val nameResolver = NameResolverImpl(packageFragment.strings, packageFragment.qualifiedNames) val nameResolver = NameResolverImpl(packageFragment.strings, packageFragment.qualifiedNames)
KmModuleFragment().apply { packageFragment.accept(this, nameResolver, listOf(fileIndex)) } packageFragment.toKmModuleFragment(nameResolver, listOf(fileIndex))
}.let(readStrategy::processModuleParts) }.let(readStrategy::processModuleParts)
} }
return KlibModuleMetadata(moduleHeader.moduleName, moduleFragments, moduleHeader.annotation) return KlibModuleMetadata(moduleHeader.moduleName, moduleFragments, moduleHeader.annotation)
@@ -119,9 +119,9 @@ class KlibModuleMetadata(
annotations annotations
) )
val groupedProtos = groupedFragments.mapValues { (_, fragments) -> val groupedProtos = groupedFragments.mapValues { (_, fragments) ->
fragments.map { fragments.map { mf ->
val c = WriteContext(ApproximatingStringTable(), listOf(reverseIndex)) 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. // 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. * 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. * 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 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? { override fun writeClassExtensions(extension: KmClassExtension, proto: ProtoBuf.Class.Builder, c: WriteContext): Boolean {
if (type != KlibClassExtensionVisitor.TYPE) return null if (extension.type != KlibClassExtensionVisitor.TYPE) return false
return object : KlibClassExtensionVisitor() { extension.accept(object : KlibClassExtensionVisitor() {
override fun visitAnnotation(annotation: KmAnnotation) { override fun visitAnnotation(annotation: KmAnnotation) {
proto.addExtension( proto.addExtension(
KlibMetadataProtoBuf.classAnnotation, KlibMetadataProtoBuf.classAnnotation,
@@ -191,21 +191,23 @@ internal class KlibMetadataExtensions : MetadataExtensions {
proto.setEnumEntry(entryIndex, entryProto.build()) proto.setEnumEntry(entryIndex, entryProto.build())
} }
} }
} })
return true
} }
override fun writePackageExtensions( override fun writePackageExtensions(
type: KmExtensionType, extension: KmPackageExtension,
proto: ProtoBuf.Package.Builder, proto: ProtoBuf.Package.Builder,
c: WriteContext c: WriteContext
): KmPackageExtensionVisitor? { ): Boolean {
if (type != KlibPackageExtensionVisitor.TYPE) return null if (extension.type != KlibPackageExtensionVisitor.TYPE) return false
return object : KlibPackageExtensionVisitor() { extension.accept(object : KlibPackageExtensionVisitor() {
override fun visitFqName(name: String) { override fun visitFqName(name: String) {
val nameIdx = (c.strings as StringTableImpl).getPackageFqNameIndex(FqName(name)) val nameIdx = (c.strings as StringTableImpl).getPackageFqNameIndex(FqName(name))
proto.setExtension(KlibMetadataProtoBuf.packageFqName, nameIdx) proto.setExtension(KlibMetadataProtoBuf.packageFqName, nameIdx)
} }
} })
return true
} }
override fun writeModuleFragmentExtensions( override fun writeModuleFragmentExtensions(
@@ -2,11 +2,11 @@
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.klib.impl
import kotlinx.metadata.internal.* import kotlinx.metadata.internal.*
import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.klib.KlibSourceFile import kotlinx.metadata.klib.KlibSourceFile
import org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf import org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
@@ -34,7 +34,8 @@ class KlibModuleFragmentWriter(
fun write(): ProtoBuf.PackageFragment = fun write(): ProtoBuf.PackageFragment =
t.build() t.build()
override fun visitEnd() { override fun writeModuleFragment(kmPackageFragment: KmModuleFragment) {
super.writeModuleFragment(kmPackageFragment)
// TODO: This should be moved to ModuleFragmentWriter. // TODO: This should be moved to ModuleFragmentWriter.
val (strings, qualifiedNames) = (c.strings as ApproximatingStringTable).buildProto() val (strings, qualifiedNames) = (c.strings as ApproximatingStringTable).buildProto()
@@ -2,7 +2,7 @@
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.klib
@@ -2,7 +2,7 @@
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 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. * 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>) { public class KmExtensionType(private val klass: KClass<out KmExtensionVisitor>) {
override fun equals(other: Any?): Boolean = override fun equals(other: Any?): Boolean =
other is KmExtensionType && klass == other.klass 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. * A base interface for all extension visitors.
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public interface KmExtensionVisitor { public interface KmExtensionVisitor {
/** /**
* Type of this extension visitor. * 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. * 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 public interface KmDeclarationContainerExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a class. * 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 public interface KmClassExtensionVisitor : KmDeclarationContainerExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a package fragment. * 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 public interface KmPackageExtensionVisitor : KmDeclarationContainerExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a function. * 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 public interface KmFunctionExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a property. * 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 public interface KmPropertyExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a constructor. * 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 public interface KmConstructorExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a type parameter. * 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 public interface KmTypeParameterExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a type. * 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 public interface KmTypeExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a type alias. * 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 public interface KmTypeAliasExtensionVisitor : KmExtensionVisitor
/** /**
* A visitor to visit platform-specific extensions for a value parameter. * 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 public interface KmValueParameterExtensionVisitor : KmExtensionVisitor
@@ -38,7 +38,7 @@ public interface KmDeclarationContainer {
* Precise kind of the class can be obtained via [KmClass.kind]. * 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]. * 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 { public class KmClass : KmClassVisitor(), KmDeclarationContainer {
/** /**
* Class flags, consisting of [Flag.HAS_ANNOTATIONS], visibility flag, modality flag and [Flag.Class] flags. * 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) public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmClassExtension> = internal val extensions: List<KmClassExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createClassExtension) MetadataExtensions.INSTANCES.map(MetadataExtensions::createClassExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visit(flags: Int, name: ClassName) { override fun visit(flags: Int, name: ClassName) {
this.flags = flags this.flags = flags
this.name = name 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 = override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters) KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSupertype(flags: Int): KmTypeVisitor = override fun visitSupertype(flags: Int): KmTypeVisitor =
KmType(flags).addTo(supertypes) KmType(flags).addTo(supertypes)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor = override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).addTo(functions) 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 = override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor =
KmProperty(flags, name, getterFlags, setterFlags).addTo(properties) 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 = override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor =
KmTypeAlias(flags, name).addTo(typeAliases) KmTypeAlias(flags, name).addTo(typeAliases)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstructor(flags: Int): KmConstructorVisitor = override fun visitConstructor(flags: Int): KmConstructorVisitor =
KmConstructor(flags).addTo(constructors) KmConstructor(flags).addTo(constructors)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitCompanionObject(name: String) { override fun visitCompanionObject(name: String) {
this.companionObject = name this.companionObject = name
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitNestedClass(name: String) { override fun visitNestedClass(name: String) {
nestedClasses.add(name) nestedClasses.add(name)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitEnumEntry(name: String) { override fun visitEnumEntry(name: String) {
enumEntries.add(name) enumEntries.add(name)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSealedSubclass(name: ClassName) { override fun visitSealedSubclass(name: ClassName) {
sealedSubclasses.add(name) sealedSubclasses.add(name)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitInlineClassUnderlyingPropertyName(name: String) { override fun visitInlineClassUnderlyingPropertyName(name: String) {
inlineClassUnderlyingPropertyName = name inlineClassUnderlyingPropertyName = name
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitInlineClassUnderlyingType(flags: Int): KmTypeVisitor = override fun visitInlineClassUnderlyingType(flags: Int): KmTypeVisitor =
KmType(flags).also { inlineClassUnderlyingType = it } KmType(flags).also { inlineClassUnderlyingType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers @ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor = override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes) KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor = override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements) KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmClassExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmClassExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -202,7 +202,7 @@ public class KmClass : KmClassVisitor(), KmDeclarationContainer {
* *
* @param visitor the visitor which will visit data in this class * @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) @OptIn(ExperimentalContextReceivers::class)
public fun accept(visitor: KmClassVisitor) { public fun accept(visitor: KmClassVisitor) {
visitor.visit(flags, name) 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. * 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. * 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 { public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
/** /**
* Functions in the package fragment. * Functions in the package fragment.
@@ -247,22 +247,22 @@ public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
*/ */
override val typeAliases: MutableList<KmTypeAlias> = ArrayList(0) override val typeAliases: MutableList<KmTypeAlias> = ArrayList(0)
private val extensions: List<KmPackageExtension> = internal val extensions: List<KmPackageExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createPackageExtension) MetadataExtensions.INSTANCES.map(MetadataExtensions::createPackageExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor = override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).addTo(functions) 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 = override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor =
KmProperty(flags, name, getterFlags, setterFlags).addTo(properties) 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 = override fun visitTypeAlias(flags: Int, name: String): KmTypeAliasVisitor =
KmTypeAlias(flags, name).addTo(typeAliases) KmTypeAlias(flags, name).addTo(typeAliases)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmPackageExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmPackageExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -271,7 +271,7 @@ public class KmPackage : KmPackageVisitor(), KmDeclarationContainer {
* *
* @param visitor the visitor which will visit data in this package fragment * @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) { public fun accept(visitor: KmPackageVisitor) {
functions.forEach { visitor.visitFunction(it.flags, it.name)?.let(it::accept) } 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) } 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. * Represents a synthetic class generated for a Kotlin lambda.
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmLambda : KmLambdaVisitor() { public class KmLambda : KmLambdaVisitor() {
/** /**
* Signature of the synthetic anonymous function, representing the lambda. * Signature of the synthetic anonymous function, representing the lambda.
*/ */
public lateinit var function: KmFunction public lateinit var function: KmFunction
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor = override fun visitFunction(flags: Int, name: String): KmFunctionVisitor =
KmFunction(flags, name).also { function = it } 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 * @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) { public fun accept(visitor: KmLambdaVisitor) {
visitor.visitFunction(function.flags, function.name)?.let(function::accept) visitor.visitFunction(function.flags, function.name)?.let(function::accept)
visitor.visitEnd() visitor.visitEnd()
@@ -313,7 +313,7 @@ public class KmLambda : KmLambdaVisitor() {
* Various constructor attributes can be read and manipulated via extension properties, * Various constructor attributes can be read and manipulated via extension properties,
* such as [KmConstructor.visibility] or [KmConstructor.isSecondary]. * such as [KmConstructor.visibility] or [KmConstructor.isSecondary].
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor( public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmConstructor, such as KmConstructor.visibility") public var flags: Int, @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) public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmConstructorExtension> = internal val extensions: List<KmConstructorExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createConstructorExtension) MetadataExtensions.INSTANCES.map(MetadataExtensions::createConstructorExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor = override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).addTo(valueParameters) KmValueParameter(flags, name).addTo(valueParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor = override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements) KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmConstructorExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmConstructorExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -350,7 +350,7 @@ public class KmConstructor @Deprecated(flagsCtorDeprecated) constructor(
* *
* @param visitor the visitor which will visit data in this class * @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) { public fun accept(visitor: KmConstructorVisitor) {
valueParameters.forEach { visitor.visitValueParameter(it.flags, it.name)?.let(it::accept) } valueParameters.forEach { visitor.visitValueParameter(it.flags, it.name)?.let(it::accept) }
versionRequirements.forEach { visitor.visitVersionRequirement()?.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 * @property name the name of the function
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmFunction @Deprecated(flagsCtorDeprecated) constructor( public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmFunction, such as KmFunction.visibility") public var flags: Int, @Deprecated("$flagAccessPrefix KmFunction, such as KmFunction.visibility") public var flags: Int,
public var name: String, public var name: String,
@@ -412,40 +412,40 @@ public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
@ExperimentalContracts @ExperimentalContracts
public var contract: KmContract? = null public var contract: KmContract? = null
private val extensions: List<KmFunctionExtension> = internal val extensions: List<KmFunctionExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createFunctionExtension) 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 = override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters) KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor = override fun visitReceiverParameterType(flags: Int): KmTypeVisitor =
KmType(flags).also { receiverParameterType = it } KmType(flags).also { receiverParameterType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers @ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor = override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes) KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor = override fun visitValueParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).addTo(valueParameters) KmValueParameter(flags, name).addTo(valueParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReturnType(flags: Int): KmTypeVisitor = override fun visitReturnType(flags: Int): KmTypeVisitor =
KmType(flags).also { returnType = it } KmType(flags).also { returnType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor = override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements) KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContracts @ExperimentalContracts
override fun visitContract(): KmContractVisitor = override fun visitContract(): KmContractVisitor =
KmContract().also { contract = it } KmContract().also { contract = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmFunctionExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmFunctionExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -455,7 +455,7 @@ public class KmFunction @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this function * @param visitor the visitor which will visit data in this function
*/ */
@OptIn(ExperimentalContextReceivers::class) @OptIn(ExperimentalContextReceivers::class)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmFunctionVisitor) { public fun accept(visitor: KmFunctionVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) } typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
receiverParameterType?.let { visitor.visitReceiverParameterType(it.flags)?.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 * @property name the name of the property
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmProperty @Deprecated(flagsCtorDeprecated) constructor( public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmProperty, such as KmProperty.visibility") public var flags: Int, @Deprecated("$flagAccessPrefix KmProperty, such as KmProperty.visibility") public var flags: Int,
public var name: String, public var name: String,
@@ -593,35 +593,35 @@ public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
*/ */
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0) public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmPropertyExtension> = internal val extensions: List<KmPropertyExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createPropertyExtension) 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 = override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters) KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReceiverParameterType(flags: Int): KmTypeVisitor = override fun visitReceiverParameterType(flags: Int): KmTypeVisitor =
KmType(flags).also { receiverParameterType = it } KmType(flags).also { receiverParameterType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@ExperimentalContextReceivers @ExperimentalContextReceivers
override fun visitContextReceiverType(flags: Int): KmTypeVisitor = override fun visitContextReceiverType(flags: Int): KmTypeVisitor =
KmType(flags).addTo(contextReceiverTypes) KmType(flags).addTo(contextReceiverTypes)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitSetterParameter(flags: Int, name: String): KmValueParameterVisitor = override fun visitSetterParameter(flags: Int, name: String): KmValueParameterVisitor =
KmValueParameter(flags, name).also { setterParameter = it } KmValueParameter(flags, name).also { setterParameter = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitReturnType(flags: Int): KmTypeVisitor = override fun visitReturnType(flags: Int): KmTypeVisitor =
KmType(flags).also { returnType = it } KmType(flags).also { returnType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor = override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements) KmVersionRequirement().addTo(versionRequirements)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmPropertyExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmPropertyExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -631,7 +631,7 @@ public class KmProperty @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this property * @param visitor the visitor which will visit data in this property
*/ */
@OptIn(ExperimentalContextReceivers::class) @OptIn(ExperimentalContextReceivers::class)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmPropertyVisitor) { public fun accept(visitor: KmPropertyVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) } typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
receiverParameterType?.let { visitor.visitReceiverParameterType(it.flags)?.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 * @property name the name of the type alias
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor( public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmTypeAlias, such as KmTypeAlias.visibility") public var flags: Int, @Deprecated("$flagAccessPrefix KmTypeAlias, such as KmTypeAlias.visibility") public var flags: Int,
public var name: String, public var name: String,
@@ -686,31 +686,31 @@ public class KmTypeAlias @Deprecated(flagsCtorDeprecated) constructor(
*/ */
public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0) public val versionRequirements: MutableList<KmVersionRequirement> = ArrayList(0)
private val extensions: List<KmTypeAliasExtension> = internal val extensions: List<KmTypeAliasExtension> =
MetadataExtensions.INSTANCES.mapNotNull(MetadataExtensions::createTypeAliasExtension) 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 = override fun visitTypeParameter(flags: Int, name: String, id: Int, variance: KmVariance): KmTypeParameterVisitor =
KmTypeParameter(flags, name, id, variance).addTo(typeParameters) KmTypeParameter(flags, name, id, variance).addTo(typeParameters)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitUnderlyingType(flags: Int): KmTypeVisitor = override fun visitUnderlyingType(flags: Int): KmTypeVisitor =
KmType(flags).also { underlyingType = it } KmType(flags).also { underlyingType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExpandedType(flags: Int): KmTypeVisitor = override fun visitExpandedType(flags: Int): KmTypeVisitor =
KmType(flags).also { expandedType = it } KmType(flags).also { expandedType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAnnotation(annotation: KmAnnotation) { override fun visitAnnotation(annotation: KmAnnotation) {
annotations.add(annotation) annotations.add(annotation)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeAliasExtensionVisitor? = override fun visitExtensions(type: KmExtensionType): KmTypeAliasExtensionVisitor? =
extensions.singleOfType(type) extensions.singleOfType(type)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersionRequirement(): KmVersionRequirementVisitor = override fun visitVersionRequirement(): KmVersionRequirementVisitor =
KmVersionRequirement().addTo(versionRequirements) 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 * @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) { public fun accept(visitor: KmTypeAliasVisitor) {
typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) } typeParameters.forEach { visitor.visitTypeParameter(it.flags, it.name, it.id, it.variance)?.let(it::accept) }
visitor.visitUnderlyingType(underlyingType.flags)?.let(underlyingType::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 * @property name the name of the value parameter
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor( public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmValueParameter, such as KmValueParameter.declaresDefaultValue") public var flags: Int, @Deprecated("$flagAccessPrefix KmValueParameter, such as KmValueParameter.declaresDefaultValue") public var flags: Int,
public var name: String, public var name: String,
@@ -758,18 +758,18 @@ public class KmValueParameter @Deprecated(flagsCtorDeprecated) constructor(
*/ */
public var varargElementType: KmType? = null public var varargElementType: KmType? = null
private val extensions: List<KmValueParameterExtension> = internal val extensions: List<KmValueParameterExtension> =
MetadataExtensions.INSTANCES.mapNotNull(MetadataExtensions::createValueParameterExtension) MetadataExtensions.INSTANCES.mapNotNull(MetadataExtensions::createValueParameterExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitType(flags: Int): KmTypeVisitor = override fun visitType(flags: Int): KmTypeVisitor =
KmType(flags).also { type = it } KmType(flags).also { type = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVarargElementType(flags: Int): KmTypeVisitor = override fun visitVarargElementType(flags: Int): KmTypeVisitor =
KmType(flags).also { varargElementType = it } KmType(flags).also { varargElementType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmValueParameterExtensionVisitor? = override fun visitExtensions(type: KmExtensionType): KmValueParameterExtensionVisitor? =
extensions.singleOfType(type) 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 * @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) { public fun accept(visitor: KmValueParameterVisitor) {
visitor.visitType(type.flags)?.let(type::accept) visitor.visitType(type.flags)?.let(type::accept)
varargElementType?.let { visitor.visitVarargElementType(it.flags)?.let(it::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) }`) * 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 * @property variance the declaration-site variance of the type parameter
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor( public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmTypeParameter, such as KmTypeParameter.isReified") public var flags: Int, @Deprecated("$flagAccessPrefix KmTypeParameter, such as KmTypeParameter.isReified") public var flags: Int,
public var name: String, public var name: String,
@@ -813,14 +813,14 @@ public class KmTypeParameter @Deprecated(flagsCtorDeprecated) constructor(
*/ */
public val upperBounds: MutableList<KmType> = ArrayList(1) public val upperBounds: MutableList<KmType> = ArrayList(1)
private val extensions: List<KmTypeParameterExtension> = internal val extensions: List<KmTypeParameterExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeParameterExtension) MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeParameterExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitUpperBound(flags: Int): KmTypeVisitor = override fun visitUpperBound(flags: Int): KmTypeVisitor =
KmType(flags).addTo(upperBounds) KmType(flags).addTo(upperBounds)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeParameterExtensionVisitor? = override fun visitExtensions(type: KmExtensionType): KmTypeParameterExtensionVisitor? =
extensions.singleOfType(type) 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 * @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) { public fun accept(visitor: KmTypeParameterVisitor) {
upperBounds.forEach { visitor.visitUpperBound(it.flags)?.let(it::accept) } upperBounds.forEach { visitor.visitUpperBound(it.flags)?.let(it::accept) }
extensions.forEach { visitor.visitExtensions(it.type)?.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, * Various type attributes can be read and manipulated via extension properties,
* such as [KmType.isNullable]. * such as [KmType.isNullable].
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmType @Deprecated(flagsCtorDeprecated) constructor( public class KmType @Deprecated(flagsCtorDeprecated) constructor(
@Deprecated("$flagAccessPrefix KmType, such as KmType.isNullable") public var flags: Int @Deprecated("$flagAccessPrefix KmType, such as KmType.isNullable") public var flags: Int
) : KmTypeVisitor() { ) : KmTypeVisitor() {
@@ -890,46 +890,46 @@ public class KmType @Deprecated(flagsCtorDeprecated) constructor(
*/ */
public var flexibleTypeUpperBound: KmFlexibleTypeUpperBound? = null public var flexibleTypeUpperBound: KmFlexibleTypeUpperBound? = null
private val extensions: List<KmTypeExtension> = internal val extensions: List<KmTypeExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeExtension) MetadataExtensions.INSTANCES.map(MetadataExtensions::createTypeExtension)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitClass(name: ClassName) { override fun visitClass(name: ClassName) {
classifier = KmClassifier.Class(name) classifier = KmClassifier.Class(name)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeAlias(name: ClassName) { override fun visitTypeAlias(name: ClassName) {
classifier = KmClassifier.TypeAlias(name) classifier = KmClassifier.TypeAlias(name)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitTypeParameter(id: Int) { override fun visitTypeParameter(id: Int) {
classifier = KmClassifier.TypeParameter(id) classifier = KmClassifier.TypeParameter(id)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitArgument(flags: Int, variance: KmVariance): KmTypeVisitor = override fun visitArgument(flags: Int, variance: KmVariance): KmTypeVisitor =
KmType(flags).also { arguments.add(KmTypeProjection(variance, it)) } KmType(flags).also { arguments.add(KmTypeProjection(variance, it)) }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitStarProjection() { override fun visitStarProjection() {
arguments.add(KmTypeProjection.STAR) arguments.add(KmTypeProjection.STAR)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAbbreviatedType(flags: Int): KmTypeVisitor = override fun visitAbbreviatedType(flags: Int): KmTypeVisitor =
KmType(flags).also { abbreviatedType = it } KmType(flags).also { abbreviatedType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOuterType(flags: Int): KmTypeVisitor = override fun visitOuterType(flags: Int): KmTypeVisitor =
KmType(flags).also { outerType = it } KmType(flags).also { outerType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitFlexibleTypeUpperBound(flags: Int, typeFlexibilityId: String?): KmTypeVisitor = override fun visitFlexibleTypeUpperBound(flags: Int, typeFlexibilityId: String?): KmTypeVisitor =
KmType(flags).also { flexibleTypeUpperBound = KmFlexibleTypeUpperBound(it, typeFlexibilityId) } KmType(flags).also { flexibleTypeUpperBound = KmFlexibleTypeUpperBound(it, typeFlexibilityId) }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmTypeExtensionVisitor = override fun visitExtensions(type: KmExtensionType): KmTypeExtensionVisitor =
extensions.singleOfType(type) extensions.singleOfType(type)
@@ -939,7 +939,7 @@ public class KmType @Deprecated(flagsCtorDeprecated) constructor(
* @param visitor the visitor which will visit data in this type * @param visitor the visitor which will visit data in this type
* @throws IllegalArgumentException if type metadata is inconsistent * @throws IllegalArgumentException if type metadata is inconsistent
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public fun accept(visitor: KmTypeVisitor) { public fun accept(visitor: KmTypeVisitor) {
when (val classifier = classifier) { when (val classifier = classifier) {
is KmClassifier.Class -> visitor.visitClass(classifier.name) 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, * 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. * enabled, for example, with the internal [kotlin.internal.RequireKotlin] annotation.
*/ */
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmVersionRequirement : KmVersionRequirementVisitor() { public class KmVersionRequirement : KmVersionRequirementVisitor() {
/** /**
* Kind of the version that this declaration requires. * Kind of the version that this declaration requires.
@@ -996,7 +996,7 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
*/ */
public lateinit var version: KmVersion 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?) { override fun visit(kind: KmVersionRequirementVersionKind, level: KmVersionRequirementLevel, errorCode: Int?, message: String?) {
this.kind = kind this.kind = kind
this.level = level this.level = level
@@ -1004,7 +1004,7 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
this.message = message this.message = message
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitVersion(major: Int, minor: Int, patch: Int) { override fun visitVersion(major: Int, minor: Int, patch: Int) {
this.version = KmVersion(major, minor, patch) 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 * @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) { public fun accept(visitor: KmVersionRequirementVisitor) {
visitor.visit(kind, level, errorCode, message) visitor.visit(kind, level, errorCode, message)
visitor.visitVersion(version.major, version.minor, version.patch) visitor.visitVersion(version.major, version.minor, version.patch)
@@ -1029,14 +1029,14 @@ public class KmVersionRequirement : KmVersionRequirementVisitor() {
* may change in a subsequent release. * may change in a subsequent release.
*/ */
@ExperimentalContracts @ExperimentalContracts
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmContract : KmContractVisitor() { public class KmContract : KmContractVisitor() {
/** /**
* Effects of this contract. * Effects of this contract.
*/ */
public val effects: MutableList<KmEffect> = ArrayList(1) 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 = override fun visitEffect(type: KmEffectType, invocationKind: KmEffectInvocationKind?): KmEffectVisitor =
KmEffect(type, invocationKind).addTo(effects) KmEffect(type, invocationKind).addTo(effects)
@@ -1045,7 +1045,7 @@ public class KmContract : KmContractVisitor() {
* *
* @param visitor the visitor which will visit data in this contract * @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) { public fun accept(visitor: KmContractVisitor) {
effects.forEach { visitor.visitEffect(it.type, it.invocationKind)?.let(it::accept) } effects.forEach { visitor.visitEffect(it.type, it.invocationKind)?.let(it::accept) }
visitor.visitEnd() visitor.visitEnd()
@@ -1063,7 +1063,7 @@ public class KmContract : KmContractVisitor() {
* specified further in the effect expression * specified further in the effect expression
*/ */
@ExperimentalContracts @ExperimentalContracts
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmEffect( public class KmEffect(
public var type: KmEffectType, public var type: KmEffectType,
public var invocationKind: KmEffectInvocationKind? public var invocationKind: KmEffectInvocationKind?
@@ -1079,11 +1079,11 @@ public class KmEffect(
*/ */
public var conclusion: KmEffectExpression? = null public var conclusion: KmEffectExpression? = null
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstructorArgument(): KmEffectExpressionVisitor = override fun visitConstructorArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(constructorArguments) KmEffectExpression().addTo(constructorArguments)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConclusionOfConditionalEffect(): KmEffectExpressionVisitor = override fun visitConclusionOfConditionalEffect(): KmEffectExpressionVisitor =
KmEffectExpression().also { conclusion = it } KmEffectExpression().also { conclusion = it }
@@ -1092,7 +1092,7 @@ public class KmEffect(
* *
* @param visitor the visitor which will visit data in this effect * @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) { public fun accept(visitor: KmEffectVisitor) {
constructorArguments.forEach { visitor.visitConstructorArgument()?.let(it::accept) } constructorArguments.forEach { visitor.visitConstructorArgument()?.let(it::accept) }
conclusion?.let { visitor.visitConclusionOfConditionalEffect()?.let(it::accept) } conclusion?.let { visitor.visitConclusionOfConditionalEffect()?.let(it::accept) }
@@ -1110,7 +1110,7 @@ public class KmEffect(
* such as [KmEffectExpression.isNegated]. * such as [KmEffectExpression.isNegated].
*/ */
@ExperimentalContracts @ExperimentalContracts
@Suppress("DEPRECATION") @Suppress("DEPRECATION", "DEPRECATION_ERROR")
public class KmEffectExpression : KmEffectExpressionVisitor() { public class KmEffectExpression : KmEffectExpressionVisitor() {
/** /**
* Effect expression flags, consisting of [Flag.EffectExpression] flags. * Effect expression flags, consisting of [Flag.EffectExpression] flags.
@@ -1146,26 +1146,26 @@ public class KmEffectExpression : KmEffectExpressionVisitor() {
*/ */
public val orArguments: MutableList<KmEffectExpression> = ArrayList(0) 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?) { override fun visit(flags: Int, parameterIndex: Int?) {
this.flags = flags this.flags = flags
this.parameterIndex = parameterIndex this.parameterIndex = parameterIndex
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitConstantValue(value: Any?) { override fun visitConstantValue(value: Any?) {
constantValue = KmConstantValue(value) constantValue = KmConstantValue(value)
} }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitIsInstanceType(flags: Int): KmTypeVisitor = override fun visitIsInstanceType(flags: Int): KmTypeVisitor =
KmType(flags).also { isInstanceType = it } KmType(flags).also { isInstanceType = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitAndArgument(): KmEffectExpressionVisitor = override fun visitAndArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(andArguments) KmEffectExpression().addTo(andArguments)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitOrArgument(): KmEffectExpressionVisitor = override fun visitOrArgument(): KmEffectExpressionVisitor =
KmEffectExpression().addTo(orArguments) KmEffectExpression().addTo(orArguments)
@@ -1174,7 +1174,7 @@ public class KmEffectExpression : KmEffectExpressionVisitor() {
* *
* @param visitor the visitor which will visit data in this effect expression * @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) { public fun accept(visitor: KmEffectExpressionVisitor) {
visitor.visit(flags, parameterIndex) visitor.visit(flags, parameterIndex)
constantValue?.let { visitor.visitConstantValue(it.value) } 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. * A visitor containing the common code to visit Kotlin declaration containers, such as classes and package fragments.
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmDeclarationContainerVisitor @JvmOverloads constructor(protected open val delegate: KmDeclarationContainerVisitor? = null) { public abstract class KmDeclarationContainerVisitor @JvmOverloads constructor(protected open val delegate: KmDeclarationContainerVisitor? = null) {
/** /**
* Visits a function in the container. * 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 * 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]. * to other visit* methods, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmClassVisitor @JvmOverloads constructor(delegate: KmClassVisitor? = null) : KmDeclarationContainerVisitor(delegate) { public abstract class KmClassVisitor @JvmOverloads constructor(delegate: KmClassVisitor? = null) : KmDeclarationContainerVisitor(delegate) {
override val delegate: KmClassVisitor? override val delegate: KmClassVisitor?
get() = super.delegate as 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. * 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)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmPackageVisitor @JvmOverloads constructor(delegate: KmPackageVisitor? = null) : public abstract class KmPackageVisitor @JvmOverloads constructor(delegate: KmPackageVisitor? = null) :
KmDeclarationContainerVisitor(delegate) { KmDeclarationContainerVisitor(delegate) {
override val delegate: KmPackageVisitor? 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]. * When using this class, [visitFunction] must be called first, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmLambdaVisitor @JvmOverloads constructor(private val delegate: KmLambdaVisitor? = null) { public abstract class KmLambdaVisitor @JvmOverloads constructor(private val delegate: KmLambdaVisitor? = null) {
/** /**
* Visits the signature of a synthetic anonymous function, representing the lambda. * 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. * 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)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmConstructorVisitor @JvmOverloads constructor(private val delegate: KmConstructorVisitor? = null) { public abstract class KmConstructorVisitor @JvmOverloads constructor(private val delegate: KmConstructorVisitor? = null) {
/** /**
* Visits a value parameter of the constructor. * 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 * 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]. * to other visit* methods, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmFunctionVisitor @JvmOverloads constructor(private val delegate: KmFunctionVisitor? = null) { public abstract class KmFunctionVisitor @JvmOverloads constructor(private val delegate: KmFunctionVisitor? = null) {
/** /**
* Visits a type parameter of the function. * 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 * 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]. * to other visit* methods, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmPropertyVisitor @JvmOverloads constructor(private val delegate: KmPropertyVisitor? = null) { public abstract class KmPropertyVisitor @JvmOverloads constructor(private val delegate: KmPropertyVisitor? = null) {
/** /**
* Visits a type parameter of the property. * 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 * 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]. * to other visit* methods, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmTypeAliasVisitor @JvmOverloads constructor(private val delegate: KmTypeAliasVisitor? = null) { public abstract class KmTypeAliasVisitor @JvmOverloads constructor(private val delegate: KmTypeAliasVisitor? = null) {
/** /**
* Visits a type parameter of the type alias. * 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 * 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]. * is `vararg` or not), followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmValueParameterVisitor @JvmOverloads constructor(private val delegate: KmValueParameterVisitor? = null) { 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. * 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]. * When using this class, zero or more [visitUpperBound] calls must be done first, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmTypeParameterVisitor @JvmOverloads constructor(private val delegate: KmTypeParameterVisitor? = null) { public abstract class KmTypeParameterVisitor @JvmOverloads constructor(private val delegate: KmTypeParameterVisitor? = null) {
/** /**
* Visits the upper bound of the type parameter. * 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. * 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)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public abstract class KmTypeVisitor @JvmOverloads constructor(private val delegate: KmTypeVisitor? = null) { 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. * 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]. * When using this class, [visit] must be called first, followed by [visitVersion], followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
public abstract class KmVersionRequirementVisitor @JvmOverloads constructor(@Suppress("DEPRECATION") private val delegate: KmVersionRequirementVisitor? = null) { public abstract class KmVersionRequirementVisitor @JvmOverloads constructor(@Suppress("DEPRECATION_ERROR") private val delegate: KmVersionRequirementVisitor? = null) {
/** /**
* Visits the description of this version requirement. * 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]. * When using this class, zero or more calls to [visitEffect] must be done first, followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
@ExperimentalContracts @ExperimentalContracts
public abstract class KmContractVisitor @JvmOverloads constructor(private val delegate: KmContractVisitor? = null) { 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, * When using this class, zero or more calls to [visitConstructorArgument] or [visitConclusionOfConditionalEffect] must be done first,
* followed by [visitEnd]. * followed by [visitEnd].
*/ */
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
@ExperimentalContracts @ExperimentalContracts
public abstract class KmEffectVisitor @JvmOverloads constructor(private val delegate: KmEffectVisitor? = null) { 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]. * 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) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
@ExperimentalContracts @ExperimentalContracts
public abstract class KmEffectExpressionVisitor @JvmOverloads constructor(private val delegate: KmEffectExpressionVisitor? = null) { 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. * 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. * 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 package kotlinx.metadata.internal
import kotlinx.metadata.* import kotlinx.metadata.*
import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.internal.extensions.MetadataExtensions import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.common.KmModuleFragmentVisitor
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.* import org.jetbrains.kotlin.metadata.deserialization.*
import kotlin.contracts.ExperimentalContracts import kotlin.contracts.ExperimentalContracts
@@ -49,11 +49,11 @@ public class ReadContext(
} }
@OptIn(ExperimentalContextReceivers::class) @OptIn(ExperimentalContextReceivers::class)
public fun ProtoBuf.Class.accept( public fun ProtoBuf.Class.toKmClass(
v: KmClassVisitor,
strings: NameResolver, strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList() contextExtensions: List<ReadContextExtension> = emptyList(),
) { ): KmClass {
val v = KmClass()
val c = ReadContext( val c = ReadContext(
strings, strings,
TypeTable(typeTable), TypeTable(typeTable),
@@ -61,61 +61,36 @@ public fun ProtoBuf.Class.accept(
contextExtensions = contextExtensions contextExtensions = contextExtensions
).withTypeParameters(typeParameterList) ).withTypeParameters(typeParameterList)
v.visit(flags, c.className(fqName)) v.flags = flags
v.name = 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) }
}
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) v.visitDeclarations(functionList, propertyList, typeAliasList, c)
if (hasCompanionObjectName()) { if (hasCompanionObjectName()) {
v.visitCompanionObject(c[companionObjectName]) v.companionObject = c[companionObjectName]
}
for (nestedClassName in nestedClassNameList) {
v.visitNestedClass(c[nestedClassName])
} }
nestedClassNameList.mapTo(v.nestedClasses) { c[it] }
for (enumEntry in enumEntryList) { for (enumEntry in enumEntryList) {
if (!enumEntry.hasName()) { if (!enumEntry.hasName()) throw InconsistentKotlinMetadataException("No name for EnumEntry")
throw InconsistentKotlinMetadataException("No name for EnumEntry") v.enumEntries.add(c[enumEntry.name])
}
v.visitEnumEntry(c[enumEntry.name])
} }
sealedSubclassFqNameList.mapTo(v.sealedSubclasses) { c.className(it) }
for (sealedSubclassFqName in sealedSubclassFqNameList) {
v.visitSealedSubclass(c.className(sealedSubclassFqName))
}
if (hasInlineClassUnderlyingPropertyName()) { if (hasInlineClassUnderlyingPropertyName()) {
v.visitInlineClassUnderlyingPropertyName(c[inlineClassUnderlyingPropertyName]) v.inlineClassUnderlyingPropertyName = c[inlineClassUnderlyingPropertyName]
}
loadInlineClassUnderlyingType(c)?.let { underlyingType ->
v.visitInlineClassUnderlyingType(underlyingType.typeFlags)?.let { underlyingType.accept(it, c) }
} }
v.inlineClassUnderlyingType = loadInlineClassUnderlyingType(c)?.toKmType(c)
for (contextReceiverType in contextReceiverTypes(c.types)) { contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(c) }
v.visitContextReceiverType(contextReceiverType.typeFlags)?.let { contextReceiverType.accept(it, c) } versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
}
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readClassExtensions(v, this, c) extension.readClassExtensions(v, this, c)
} }
v.visitEnd() return v
} }
private fun ProtoBuf.Class.loadInlineClassUnderlyingType(c: ReadContext): ProtoBuf.Type? { private fun ProtoBuf.Class.loadInlineClassUnderlyingType(c: ReadContext): ProtoBuf.Type? {
@@ -130,11 +105,11 @@ private fun ProtoBuf.Class.loadInlineClassUnderlyingType(c: ReadContext): ProtoB
?.returnType(c.types) ?.returnType(c.types)
} }
public fun ProtoBuf.Package.accept( public fun ProtoBuf.Package.toKmPackage(
v: KmPackageVisitor,
strings: NameResolver, strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList() contextExtensions: List<ReadContextExtension> = emptyList(),
) { ): KmPackage {
val v = KmPackage()
val c = ReadContext( val c = ReadContext(
strings, strings,
TypeTable(typeTable), TypeTable(typeTable),
@@ -148,14 +123,14 @@ public fun ProtoBuf.Package.accept(
extension.readPackageExtensions(v, this, c) extension.readPackageExtensions(v, this, c)
} }
v.visitEnd() return v
} }
public fun ProtoBuf.PackageFragment.accept( public fun ProtoBuf.PackageFragment.toKmModuleFragment(
v: KmModuleFragmentVisitor,
strings: NameResolver, strings: NameResolver,
contextExtensions: List<ReadContextExtension> = emptyList() contextExtensions: List<ReadContextExtension> = emptyList(),
) { ): KmModuleFragment {
val v = KmModuleFragment()
val c = ReadContext( val c = ReadContext(
strings, strings,
TypeTable(ProtoBuf.TypeTable.newBuilder().build()), TypeTable(ProtoBuf.TypeTable.newBuilder().build()),
@@ -163,223 +138,155 @@ public fun ProtoBuf.PackageFragment.accept(
contextExtensions = contextExtensions contextExtensions = contextExtensions
) )
v.visitPackage()?.let { `package`.accept(it, strings, contextExtensions) } v.pkg = `package`.toKmPackage(strings, contextExtensions)
class_List.mapTo(v.classes) { it.toKmClass(strings, contextExtensions) }
class_List.forEach { clazz ->
v.visitClass()?.let { clazz.accept(it, strings, contextExtensions) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readModuleFragmentExtensions(v, this, c) extension.readModuleFragmentExtensions(v, this, c)
} }
v.visitEnd() return v
} }
private fun KmDeclarationContainerVisitor.visitDeclarations( private fun KmDeclarationContainer.visitDeclarations(
functions: List<ProtoBuf.Function>, protoFunctions: List<ProtoBuf.Function>,
properties: List<ProtoBuf.Property>, protoProperties: List<ProtoBuf.Property>,
typeAliases: List<ProtoBuf.TypeAlias>, protoTypeAliases: List<ProtoBuf.TypeAlias>,
c: ReadContext c: ReadContext,
) { ) {
for (function in functions) { protoFunctions.mapTo(functions) { it.toKmFunction(c) }
visitFunction(function.flags, c[function.name])?.let { function.accept(it, c) } protoProperties.mapTo(properties) { it.toKmProperty(c) }
} protoTypeAliases.mapTo(typeAliases) { it.toKmTypeAlias(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) }
}
} }
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) val c = ReadContext(strings, TypeTable(typeTable), VersionRequirementTable.EMPTY)
v.function = this.toKmFunction(c)
v.visitFunction(flags, c[name])?.let { accept(it, c) } return v
v.visitEnd()
} }
private fun ProtoBuf.Constructor.accept(v: KmConstructorVisitor, c: ReadContext) { private fun ProtoBuf.Constructor.toKmConstructor(c: ReadContext): KmConstructor {
for (parameter in valueParameterList) { val v = KmConstructor(flags)
v.visitValueParameter(parameter.flags, c[parameter.name])?.let { parameter.accept(it, c) } valueParameterList.mapTo(v.valueParameters) { it.toKmValueParameter(c) }
} versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (versionRequirement in versionRequirementList) {
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readConstructorExtensions(v, this, c) extension.readConstructorExtensions(v, this, c)
} }
v.visitEnd() return v
} }
@OptIn(ExperimentalContextReceivers::class) @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) val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) { typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
typeParameter.accept(v::visitTypeParameter, c) v.receiverParameterType = receiverType(c.types)?.toKmType(c)
} contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(c) }
valueParameterList.mapTo(v.valueParameters) { it.toKmValueParameter(c) }
receiverType(c.types)?.let { receiverType -> v.returnType = returnType(c.types).toKmType(c)
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) }
}
@OptIn(ExperimentalContracts::class)
if (hasContract()) { if (hasContract()) {
@OptIn(ExperimentalContracts::class) v.visitContract()?.let { contract.accept(it, c) } v.contract = contract.toKmContract(c)
} }
for (versionRequirement in versionRequirementList) { versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
v.visitVersionRequirement()?.let { acceptVersionRequirementVisitor(versionRequirement, it, c) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readFunctionExtensions(v, this, c) extension.readFunctionExtensions(v, this, c)
} }
v.visitEnd() return v
} }
@OptIn(ExperimentalContextReceivers::class) @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) val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) { typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
typeParameter.accept(v::visitTypeParameter, c) v.receiverParameterType = receiverType(c.types)?.toKmType(c)
} contextReceiverTypes(c.types).mapTo(v.contextReceiverTypes) { it.toKmType(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) }
}
if (hasSetterValueParameter()) { if (hasSetterValueParameter()) {
val parameter = setterValueParameter v.setterParameter = setterValueParameter.toKmValueParameter(c)
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.returnType = returnType(c.types).toKmType(c)
versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readPropertyExtensions(v, this, c) 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) val c = outer.withTypeParameters(typeParameterList)
for (typeParameter in typeParameterList) { typeParameterList.mapTo(v.typeParameters) { it.toKmTypeParameter(c) }
typeParameter.accept(v::visitTypeParameter, 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 -> versionRequirementList.mapTo(v.versionRequirements) { readVersionRequirement(it, c) }
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) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readTypeAliasExtensions(v, this, c) extension.readTypeAliasExtensions(v, this, c)
} }
v.visitEnd() return v
} }
private fun ProtoBuf.ValueParameter.accept(v: KmValueParameterVisitor, c: ReadContext) { private fun ProtoBuf.ValueParameter.toKmValueParameter(c: ReadContext): KmValueParameter {
type(c.types).let { type -> val v = KmValueParameter(flags, c[name])
v.visitType(type.typeFlags)?.let { type.accept(it, c) } v.type = type(c.types).toKmType(c)
}
varargElementType(c.types)?.let { varargElementType -> v.varargElementType = varargElementType(c.types)?.toKmType(c)
v.visitVarargElementType(varargElementType.typeFlags)?.let { varargElementType.accept(it, c) }
}
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readValueParameterExtensions(v, this, c) extension.readValueParameterExtensions(v, this, c)
} }
v.visitEnd() return v
} }
private inline fun ProtoBuf.TypeParameter.accept( private fun ProtoBuf.TypeParameter.toKmTypeParameter(
visit: (flags: Int, name: String, id: Int, variance: KmVariance) -> KmTypeParameterVisitor?, c: ReadContext,
c: ReadContext ): KmTypeParameter {
) {
val variance = when (requireNotNull(variance)) { val variance = when (requireNotNull(variance)) {
ProtoBuf.TypeParameter.Variance.IN -> KmVariance.IN ProtoBuf.TypeParameter.Variance.IN -> KmVariance.IN
ProtoBuf.TypeParameter.Variance.OUT -> KmVariance.OUT ProtoBuf.TypeParameter.Variance.OUT -> KmVariance.OUT
ProtoBuf.TypeParameter.Variance.INV -> KmVariance.INVARIANT ProtoBuf.TypeParameter.Variance.INV -> KmVariance.INVARIANT
} }
val ktp = KmTypeParameter(typeParameterFlags, c[name], id, variance)
visit(typeParameterFlags, c[name], id, variance)?.let { accept(it, c) } upperBounds(c.types).mapTo(ktp.upperBounds) { it.toKmType(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) }
}
for (extension in c.extensions) { 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) { private fun ProtoBuf.Type.toKmType(c: ReadContext): KmType {
when { val v = KmType(typeFlags)
hasClassName() -> v.visitClass(c.className(className)) v.classifier = when {
hasTypeAliasName() -> v.visitTypeAlias(c.className(typeAliasName)) hasClassName() -> KmClassifier.Class(c.className(className))
hasTypeParameter() -> v.visitTypeParameter(typeParameter) hasTypeAliasName() -> KmClassifier.TypeAlias(c.className(typeAliasName))
hasTypeParameter() -> KmClassifier.TypeParameter(typeParameter)
hasTypeParameterName() -> { hasTypeParameterName() -> {
val id = c.getTypeParameterId(typeParameterName) val id = c.getTypeParameterId(typeParameterName)
?: throw InconsistentKotlinMetadataException("No type parameter id for ${c[typeParameterName]}") ?: throw InconsistentKotlinMetadataException("No type parameter id for ${c[typeParameterName]}")
v.visitTypeParameter(id) KmClassifier.TypeParameter(id)
}
else -> {
throw InconsistentKotlinMetadataException("No classifier (class, type alias or type parameter) recorded for Type")
} }
else -> throw InconsistentKotlinMetadataException("No classifier (class, type alias or type parameter) recorded for Type")
} }
for (argument in argumentList) { for (argument in argumentList) {
@@ -393,35 +300,28 @@ private fun ProtoBuf.Type.accept(v: KmTypeVisitor, c: ReadContext) {
if (variance != null) { if (variance != null) {
val argumentType = argument.type(c.types) val argumentType = argument.type(c.types)
?: throw InconsistentKotlinMetadataException("No type argument for non-STAR projection in Type") ?: 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 { } else {
v.visitStarProjection() v.arguments.add(KmTypeProjection.STAR)
} }
} }
abbreviatedType(c.types)?.let { abbreviatedType -> v.abbreviatedType = abbreviatedType(c.types)?.toKmType(c)
v.visitAbbreviatedType(abbreviatedType.typeFlags)?.let { abbreviatedType.accept(it, c) } v.outerType = outerType(c.types)?.toKmType(c)
}
outerType(c.types)?.let { outerType -> v.flexibleTypeUpperBound = flexibleUpperBound(c.types)?.toKmType(c)?.let {
v.visitOuterType(outerType.typeFlags)?.let { outerType.accept(it, c) } KmFlexibleTypeUpperBound(it, if (hasFlexibleTypeCapabilitiesId()) c[flexibleTypeCapabilitiesId] else null)
}
flexibleUpperBound(c.types)?.let { upperBound ->
v.visitFlexibleTypeUpperBound(
upperBound.typeFlags,
if (hasFlexibleTypeCapabilitiesId()) c[flexibleTypeCapabilitiesId] else null
)?.let { upperBound.accept(it, c) }
} }
for (extension in c.extensions) { for (extension in c.extensions) {
extension.readTypeExtensions(v, this, c) 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) val message = VersionRequirement.create(id, c.strings, c.versionRequirements)
?: throw InconsistentKotlinMetadataException("No VersionRequirement with the given id in the table") ?: 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 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 val (major, minor, patch) = message.version
v.visitVersion(major, minor, patch) v.version = KmVersion(major, minor, patch)
return v
v.visitEnd()
} }
@ExperimentalContracts @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) { for (effect in effectList) {
if (!effect.hasEffectType()) continue 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 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 @ExperimentalContracts
private fun ProtoBuf.Effect.accept(v: KmEffectVisitor, c: ReadContext) { private fun ProtoBuf.Effect.toKmEffect(type: KmEffectType, kind: KmEffectInvocationKind?, c: ReadContext): KmEffect {
for (constructorArgument in effectConstructorArgumentList) { val v = KmEffect(type, kind)
v.visitConstructorArgument()?.let { constructorArgument.accept(it, c) } effectConstructorArgumentList.mapTo(v.constructorArguments) { it.toKmEffectExpression(c) }
}
if (hasConclusionOfConditionalEffect()) { if (hasConclusionOfConditionalEffect()) {
v.visitConclusionOfConditionalEffect()?.let { conclusionOfConditionalEffect.accept(it, c) } v.conclusion = conclusionOfConditionalEffect.toKmEffectExpression(c)
} }
v.visitEnd() return v
} }
@ExperimentalContracts @ExperimentalContracts
private fun ProtoBuf.Expression.accept(v: KmEffectExpressionVisitor, c: ReadContext) { private fun ProtoBuf.Expression.toKmEffectExpression(c: ReadContext): KmEffectExpression {
v.visit( val v = KmEffectExpression()
flags, v.flags = flags
if (hasValueParameterReference()) valueParameterReference else null v.parameterIndex = if (hasValueParameterReference()) valueParameterReference else null
)
if (hasConstantValue()) { if (hasConstantValue()) {
v.visitConstantValue( v.constantValue = KmConstantValue(
when (requireNotNull(constantValue)) { when (requireNotNull(constantValue)) {
ProtoBuf.Expression.ConstantValue.TRUE -> true ProtoBuf.Expression.ConstantValue.TRUE -> true
ProtoBuf.Expression.ConstantValue.FALSE -> false ProtoBuf.Expression.ConstantValue.FALSE -> false
@@ -498,19 +399,12 @@ private fun ProtoBuf.Expression.accept(v: KmEffectExpressionVisitor, c: ReadCont
) )
} }
isInstanceType(c.types)?.let { type -> v.isInstanceType = isInstanceType(c.types)?.toKmType(c)
v.visitIsInstanceType(type.typeFlags)?.let { type.accept(it, c) }
}
for (andArgument in andArgumentList) { andArgumentList.mapTo(v.andArguments) { it.toKmEffectExpression(c) }
v.visitAndArgument()?.let { andArgument.accept(it, c) } orArgumentList.mapTo(v.orArguments) { it.toKmEffectExpression(c) }
}
for (orArgument in orArgumentList) { return v
v.visitOrArgument()?.let { orArgument.accept(it, c) }
}
v.visitEnd()
} }
private val ProtoBuf.Type.typeFlags: Int private val ProtoBuf.Type.typeFlags: Int
@@ -5,10 +5,10 @@
package kotlinx.metadata.internal package kotlinx.metadata.internal
import kotlinx.metadata.ClassName import kotlinx.metadata.*
import kotlinx.metadata.KmAnnotation import kotlinx.metadata.internal.extensions.KmExtension
import kotlinx.metadata.KmAnnotationArgument import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.isLocalClassName import kotlinx.metadata.internal.extensions.applySingleExtension
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.Flags import org.jetbrains.kotlin.metadata.deserialization.Flags
import org.jetbrains.kotlin.metadata.serialization.StringTable import org.jetbrains.kotlin.metadata.serialization.StringTable
@@ -115,3 +115,9 @@ internal fun StringTable.getClassNameIndex(name: ClassName): Int =
getQualifiedClassNameIndex(name.substring(1), true) getQualifiedClassNameIndex(name.substring(1), true)
else else
getQualifiedClassNameIndex(name, false) 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. * 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. * 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 package kotlinx.metadata.internal
import kotlinx.metadata.* import kotlinx.metadata.*
import kotlinx.metadata.internal.common.KmModuleFragmentExtensionVisitor import kotlinx.metadata.internal.common.KmModuleFragment
import kotlinx.metadata.internal.common.KmModuleFragmentVisitor import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.extensions.applySingleExtension import kotlinx.metadata.internal.extensions.matchExtensions
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.VersionRequirement import org.jetbrains.kotlin.metadata.deserialization.VersionRequirement
import org.jetbrains.kotlin.metadata.serialization.MutableVersionRequirementTable 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) strings.getClassNameIndex(name)
} }
private fun writeTypeParameter( private fun WriteContext.writeTypeParameter(kmTypeParameter: KmTypeParameter): ProtoBuf.TypeParameter.Builder {
c: WriteContext, flags: Int, name: String, id: Int, variance: KmVariance, val t = ProtoBuf.TypeParameter.newBuilder()
output: (ProtoBuf.TypeParameter.Builder) -> Unit kmTypeParameter.upperBounds.forEach { ub ->
): KmTypeParameterVisitor = t.addUpperBound(writeType(ub).build())
object : KmTypeParameterVisitor() { }
private val t = ProtoBuf.TypeParameter.newBuilder() 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? = private fun WriteContext.writeTypeProjection(argument: KmTypeProjection): ProtoBuf.Type.Argument.Builder {
writeType(c, flags) { t.addUpperBound(it) } val t = ProtoBuf.Type.Argument.newBuilder()
if (argument == KmTypeProjection.STAR) {
override fun visitExtensions(type: KmExtensionType): KmTypeParameterExtensionVisitor? = t.projection = ProtoBuf.Type.Argument.Projection.STAR
applySingleExtension(type) { } else {
writeTypeParameterExtensions(type, t, c) val (variance, argType) = argument
} if (variance == null || argType == null)
throw InconsistentKotlinMetadataException("Variance and type must be set for non-star type projection")
override fun visitEnd() { if (variance == KmVariance.IN) {
t.name = c[name] t.projection = ProtoBuf.Type.Argument.Projection.IN
t.id = id } else if (variance == KmVariance.OUT) {
val reified = Flag.TypeParameter.IS_REIFIED(flags) t.projection = ProtoBuf.Type.Argument.Projection.OUT
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)
} }
t.type = writeType(argType).build()
} }
private fun writeType(c: WriteContext, flags: Int, output: (ProtoBuf.Type.Builder) -> Unit): KmTypeVisitor = return t
object : KmTypeVisitor() { }
private val t = ProtoBuf.Type.newBuilder()
override fun visitClass(name: ClassName) { private fun WriteContext.writeType(kmType: KmType): ProtoBuf.Type.Builder {
t.className = c.getClassName(name) 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
} }
if (versionKind != defaultInstanceForType.versionKind) {
override fun visitTypeAlias(name: ClassName) { this.versionKind = versionKind
t.typeAliasName = c.getClassName(name)
} }
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() { VersionRequirement.Version(major, minor, patch).encode(
t.addArgument(ProtoBuf.Type.Argument.newBuilder().apply { writeVersion = { t!!.version = it },
projection = ProtoBuf.Type.Argument.Projection.STAR 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? = t.addAllSealedSubclassFqName(kmClass.sealedSubclasses.map { c.getClassName(it) })
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()
})
}
override fun visitTypeParameter(id: Int) { kmClass.inlineClassUnderlyingPropertyName?.let { t.inlineClassUnderlyingPropertyName = c[it] }
t.typeParameter = id kmClass.inlineClassUnderlyingType?.let { t.inlineClassUnderlyingType = c.writeType(it).build() }
}
override fun visitAbbreviatedType(flags: Int): KmTypeVisitor? = @OptIn(ExperimentalContextReceivers::class)
writeType(c, flags) { t.abbreviatedType = it.build() } t.addAllContextReceiverType(kmClass.contextReceiverTypes.map { c.writeType(it).build() })
override fun visitOuterType(flags: Int): KmTypeVisitor? = t.addAllVersionRequirement(kmClass.versionRequirements.map { c.writeVersionRequirement(it) })
writeType(c, flags) { t.outerType = it.build() } 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 { c.versionRequirements.serialize()?.let {
t.versionRequirementTable = it t.versionRequirementTable = it
} }
} }
} }
public open class PackageWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) : KmPackageVisitor() { public open class PackageWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) {
protected val t: ProtoBuf.Package.Builder = ProtoBuf.Package.newBuilder()!! public val t: ProtoBuf.Package.Builder = ProtoBuf.Package.newBuilder()
protected val c: WriteContext = WriteContext(stringTable, contextExtensions) public val c: WriteContext = WriteContext(stringTable, contextExtensions)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor? = public fun writePackage(kmPackage: KmPackage) {
writeFunction(c, flags, name) { t.addFunction(it) } t.addAllFunction(kmPackage.functions.map { c.writeFunction(it).build() })
t.addAllProperty(kmPackage.properties.map { c.writeProperty(it).build() })
override fun visitProperty(flags: Int, name: String, getterFlags: Int, setterFlags: Int): KmPropertyVisitor? = t.addAllTypeAlias(kmPackage.typeAliases.map { c.writeTypeAlias(it).build() })
writeProperty(c, flags, name, getterFlags, setterFlags) { t.addProperty(it) } kmPackage.extensions.matchExtensions { writePackageExtensions(it, t, c) }
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() {
c.versionRequirements.serialize()?.let { c.versionRequirements.serialize()?.let {
t.versionRequirementTable = it t.versionRequirementTable = it
} }
} }
} }
public open class ModuleFragmentWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) : public open class ModuleFragmentWriter(stringTable: StringTable, contextExtensions: List<WriteContextExtension> = emptyList()) {
KmModuleFragmentVisitor() {
protected val t: ProtoBuf.PackageFragment.Builder = ProtoBuf.PackageFragment.newBuilder()!! protected val t: ProtoBuf.PackageFragment.Builder = ProtoBuf.PackageFragment.newBuilder()!!
protected val c: WriteContext = WriteContext(stringTable, contextExtensions) protected val c: WriteContext = WriteContext(stringTable, contextExtensions)
override fun visitPackage(): KmPackageVisitor? = object : PackageWriter(c.strings, c.contextExtensions) { public open fun writeModuleFragment(kmPackageFragment: KmModuleFragment) {
override fun visitEnd() { kmPackageFragment.pkg?.let {
super.visitEnd() val pkgWriter = PackageWriter(c.strings, c.contextExtensions)
this@ModuleFragmentWriter.t.setPackage(t) pkgWriter.writePackage(it)
t.setPackage(pkgWriter.t)
} }
}
override fun visitClass(): KmClassVisitor? = object : ClassWriter(c.strings, c.contextExtensions) { kmPackageFragment.classes.forEach {
override fun visitEnd() { val classWriter = ClassWriter(c.strings, c.contextExtensions)
super.visitEnd() classWriter.writeClass(it)
this@ModuleFragmentWriter.t.addClass_(t) t.addClass_(classWriter.t)
} }
}
override fun visitExtensions(type: KmExtensionType): KmModuleFragmentExtensionVisitor? = kmPackageFragment.extensions.writeExtensions { writeModuleFragmentExtensions(it, t, c) }
applySingleExtension(type) { }
writeModuleFragmentExtensions(type, t, c)
}
} }
public open class LambdaWriter(stringTable: StringTable) : KmLambdaVisitor() { public open class LambdaWriter(stringTable: StringTable) {
protected var t: ProtoBuf.Function.Builder? = null public var t: ProtoBuf.Function.Builder? = null
protected val c: WriteContext = WriteContext(stringTable) public val c: WriteContext = WriteContext(stringTable)
override fun visitFunction(flags: Int, name: String): KmFunctionVisitor? = public fun writeLambda(kmLambda: KmLambda) {
writeFunction(c, flags, name) { t = it } t = c.writeFunction(kmLambda.function)
}
} }
@@ -2,13 +2,14 @@
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.internal.common
import kotlinx.metadata.* import kotlinx.metadata.*
import kotlinx.metadata.VISITOR_API_MESSAGE 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.KmModuleFragmentExtension
import kotlinx.metadata.internal.extensions.MetadataExtensions import kotlinx.metadata.internal.extensions.MetadataExtensions
import kotlinx.metadata.internal.extensions.singleOfType import kotlinx.metadata.internal.extensions.singleOfType
@@ -23,23 +24,33 @@ import java.io.ByteArrayInputStream
* distribution can also be read with this reader. * distribution can also be read with this reader.
*/ */
public class KotlinCommonMetadata private constructor(private val proto: ProtoBuf.PackageFragment) { 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 = 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 because there are no use-cases and it is not finished
private class Writer : KmModuleFragmentVisitor() { private class Writer : KmModuleFragmentVisitor() {
// TODO // 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) val strings = NameResolverImpl(proto.strings, proto.qualifiedNames)
if (proto.hasPackage()) { if (proto.hasPackage()) {
v.visitPackage()?.let { proto.`package`.accept(it, strings) } v.pkg = proto.`package`.toKmPackage(strings)
} }
for (klass in proto.class_List) { proto.class_List.mapTo(v.classes) { it.toKmClass(strings) }
v.visitClass()?.let { klass.accept(it, strings) } return v
}
v.visitEnd()
} }
public companion object { public companion object {
@@ -62,7 +73,6 @@ public class KotlinCommonMetadata private constructor(private val proto: ProtoBu
* *
* Can be read with [KotlinCommonMetadata.read]. * Can be read with [KotlinCommonMetadata.read].
*/ */
@Suppress("DEPRECATION")
public class KmModuleFragment : KmModuleFragmentVisitor() { public class KmModuleFragment : KmModuleFragmentVisitor() {
/** /**
@@ -75,18 +85,18 @@ public class KmModuleFragment : KmModuleFragmentVisitor() {
*/ */
public val classes: MutableList<KmClass> = ArrayList() public val classes: MutableList<KmClass> = ArrayList()
private val extensions: List<KmModuleFragmentExtension> = internal val extensions: List<KmModuleFragmentExtension> =
MetadataExtensions.INSTANCES.map(MetadataExtensions::createModuleFragmentExtensions) MetadataExtensions.INSTANCES.map(MetadataExtensions::createModuleFragmentExtensions)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitPackage(): KmPackageVisitor? = override fun visitPackage(): KmPackageVisitor? =
KmPackage().also { pkg = it } KmPackage().also { pkg = it }
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitExtensions(type: KmExtensionType): KmModuleFragmentExtensionVisitor? = override fun visitExtensions(type: KmExtensionType): KmModuleFragmentExtensionVisitor? =
extensions.singleOfType(type) extensions.singleOfType(type)
@Deprecated(VISITOR_API_MESSAGE) @Deprecated(VISITOR_API_MESSAGE, level = DeprecationLevel.ERROR)
override fun visitClass(): KmClassVisitor? = override fun visitClass(): KmClassVisitor? =
KmClass().addTo(classes) KmClass().addTo(classes)
@@ -95,7 +105,7 @@ public class KmModuleFragment : KmModuleFragmentVisitor() {
* *
* @param visitor the visitor which will visit data in the module fragment. * @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) { public fun accept(visitor: KmModuleFragmentVisitor) {
pkg?.let { visitor.visitPackage()?.let(it::accept) } pkg?.let { visitor.visitPackage()?.let(it::accept) }
classes.forEach { visitor.visitClass()?.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. * 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)
@Suppress("DEPRECATION")
public abstract class KmModuleFragmentVisitor @JvmOverloads constructor(private val delegate: KmModuleFragmentVisitor? = null) { 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. * 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 public interface KmModuleFragmentExtensionVisitor : KmExtensionVisitor
@@ -2,7 +2,7 @@
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.internal.extensions
@@ -2,7 +2,7 @@
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. * 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. * 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 package kotlinx.metadata.internal.extensions
import kotlinx.metadata.KmExtensionType import kotlinx.metadata.KmExtensionType
@@ -20,6 +20,17 @@ internal fun <T : KmExtensionVisitor> applySingleExtension(type: KmExtensionType
return result 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 { internal fun <N : KmExtension<*>> Collection<N>.singleOfType(type: KmExtensionType): N {
var result: N? = null var result: N? = null
for (node in this) { for (node in this) {
@@ -11,7 +11,7 @@ import kotlinx.metadata.internal.common.*
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
import java.util.* import java.util.*
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
public interface MetadataExtensions { public interface MetadataExtensions {
public fun readClassExtensions(v: KmClassVisitor, proto: ProtoBuf.Class, c: ReadContext) 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 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( public fun writeModuleFragmentExtensions(
type: KmExtensionType, proto: ProtoBuf.PackageFragment.Builder, c: WriteContext 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 abbreviatedType = type.abbreviatedType?.let(::printType)
val outerType = type.outerType?.let(::printType) val outerType = type.outerType?.let(::printType)
val platformTypeUpperBound = type.flexibleTypeUpperBound?.let { val platformTypeUpperBound = type.flexibleTypeUpperBound?.let {
@Suppress("DEPRECATION") @Suppress("DEPRECATION_ERROR")
(if (it.typeFlexibilityId == JvmTypeExtensionVisitor.PLATFORM_TYPE_ID) { (if (it.typeFlexibilityId == JvmTypeExtensionVisitor.PLATFORM_TYPE_ID) {
printType(it.type) printType(it.type)
} else null) } else null)
@@ -60,7 +60,6 @@ private fun compileAndPrintAllFiles(
useK2: Boolean, useK2: Boolean,
) { ) {
val main = StringBuilder() val main = StringBuilder()
val afterVisitors = StringBuilder()
val afterNodes = StringBuilder() val afterNodes = StringBuilder()
val kotlinp = Kotlinp(KotlinpSettings(isVerbose = true, sortDeclarations = true)) val kotlinp = Kotlinp(KotlinpSettings(isVerbose = true, sortDeclarations = true))
@@ -70,11 +69,10 @@ private fun compileAndPrintAllFiles(
when (outputFile.extension) { when (outputFile.extension) {
"kotlin_module" -> { "kotlin_module" -> {
val moduleFile = kotlinp.readModuleFile(outputFile)!! val moduleFile = kotlinp.readModuleFile(outputFile)!!
val transformedWithVisitors = KotlinModuleMetadata.read(transformModuleFileWithReadWriteVisitors(moduleFile))
val transformedWithNodes = KotlinModuleMetadata.read(transformModuleFileWithNodes(moduleFile)) val transformedWithNodes = KotlinModuleMetadata.read(transformModuleFileWithNodes(moduleFile))
for ((sb, moduleFileToRender) in listOf( 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.appendFileName(outputFile.relativeTo(tmpdir))
sb.append(kotlinp.renderModuleFile(moduleFileToRender)) sb.append(kotlinp.renderModuleFile(moduleFileToRender))
@@ -83,11 +81,10 @@ private fun compileAndPrintAllFiles(
"class" -> { "class" -> {
val metadata = kotlinp.readClassFile(outputFile) val metadata = kotlinp.readClassFile(outputFile)
val classFile = kotlinp.readMetadata(metadata) val classFile = kotlinp.readMetadata(metadata)
val classFile2 = transformClassFileWithReadWriteVisitors(classFile)
val classFile3 = KotlinClassMetadata.read(transformClassFileWithNodes(metadata, classFile)) val classFile3 = KotlinClassMetadata.read(transformClassFileWithNodes(metadata, classFile))
for ((sb, classFileToRender) in listOf( 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.appendFileName(outputFile.relativeTo(tmpdir))
sb.append(kotlinp.renderClassFile(classFileToRender)) sb.append(kotlinp.renderClassFile(classFileToRender))
@@ -102,7 +99,6 @@ private fun compileAndPrintAllFiles(
} }
if (readWriteAndCompare && InTextDirectivesUtils.findStringWithPrefixes(file.readText(), "// NO_READ_WRITE_COMPARE") == null) { 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()) assertEquals("Metadata is different after transformation with nodes.", main.toString(), afterNodes.toString())
} }
} }
@@ -130,27 +126,6 @@ private fun StringBuilder.appendFileName(file: File) {
appendLine("// ------------------------------------------") 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. // Reads the class file and writes it back with KmClass/KmFunction/... elements.
private fun transformClassFileWithNodes(metadata: Metadata, classFile: KotlinClassMetadata): Metadata = private fun transformClassFileWithNodes(metadata: Metadata, classFile: KotlinClassMetadata): Metadata =
when (classFile) { when (classFile) {
@@ -166,11 +141,6 @@ private fun transformClassFileWithNodes(metadata: Metadata, classFile: KotlinCla
is KotlinClassMetadata.Unknown -> metadata 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) @OptIn(UnstableMetadataApi::class)
private fun transformModuleFileWithNodes(moduleFile: KotlinModuleMetadata): ByteArray = private fun transformModuleFileWithNodes(moduleFile: KotlinModuleMetadata): ByteArray =
KotlinModuleMetadata.write(moduleFile.kmModule) KotlinModuleMetadata.write(moduleFile.kmModule)