[Commonizer] Move all CIR deserialization logic to CirDeserializers.kt

This commit is contained in:
Dmitriy Dolovov
2021-03-05 01:42:51 +03:00
parent 1737bdd4d4
commit d07ede7305
23 changed files with 455 additions and 650 deletions
@@ -1,71 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import gnu.trove.THashMap
import kotlinx.metadata.Flag
import kotlinx.metadata.Flags
import kotlinx.metadata.KmAnnotation
import kotlinx.metadata.KmAnnotationArgument
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.utils.compact
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirAnnotationFactory {
fun createAnnotations(flags: Flags, typeResolver: CirTypeResolver, annotations: () -> List<KmAnnotation>): List<CirAnnotation> {
return if (!Flag.Common.HAS_ANNOTATIONS(flags))
emptyList()
else
annotations().compactMap { create(it, typeResolver) }
}
fun create(source: KmAnnotation, typeResolver: CirTypeResolver): CirAnnotation {
val classId = CirEntityId.create(source.className)
val clazz: CirProvided.Class = typeResolver.resolveClassifier(classId)
val type = CirClassType.createInterned(
classId = classId,
outerType = null, // annotation class can't be inner class
visibility = clazz.visibility,
arguments = clazz.typeParameters.compactMap { typeParameter ->
CirTypeProjectionImpl(
projectionKind = typeParameter.variance,
type = CirTypeParameterType.createInterned(
index = typeParameter.index,
isMarkedNullable = false
)
)
},
isMarkedNullable = false
)
val allValueArguments: Map<String, KmAnnotationArgument<*>> = source.arguments
if (allValueArguments.isEmpty())
return CirAnnotation.createInterned(type = type, constantValueArguments = emptyMap(), annotationValueArguments = emptyMap())
val constantValueArguments: MutableMap<CirName, CirConstantValue<*>> = THashMap(allValueArguments.size)
val annotationValueArguments: MutableMap<CirName, CirAnnotation> = THashMap(allValueArguments.size)
allValueArguments.forEach { (name, constantValue) ->
val cirName = CirName.create(name)
if (constantValue is KmAnnotationArgument.AnnotationValue)
annotationValueArguments[cirName] = create(source = constantValue.value, typeResolver)
else
constantValueArguments[cirName] = CirConstantValueFactory.createSafely(
constantValue = constantValue,
constantName = cirName,
owner = source,
)
}
return CirAnnotation.createInterned(
type = type,
constantValueArguments = constantValueArguments.compact(),
annotationValueArguments = annotationValueArguments.compact()
)
}
}
@@ -1,27 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmConstructor
import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassConstructor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirContainingClass
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirClassConstructorFactory {
fun create(source: KmConstructor, containingClass: CirContainingClass, typeResolver: CirTypeResolver): CirClassConstructor =
CirClassConstructor.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
typeParameters = emptyList(), // TODO: nowhere to read constructor type parameters from
visibility = decodeVisibility(source.flags),
containingClass = containingClass,
valueParameters = source.valueParameters.compactMap { CirValueParameterFactory.create(it, typeResolver) },
hasStableParameterNames = !Flag.Constructor.HAS_NON_STABLE_PARAMETER_NAMES(source.flags),
isPrimary = !Flag.Constructor.IS_SECONDARY(source.flags)
)
}
@@ -1,72 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmAnnotation
import kotlinx.metadata.KmClass
import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeClassKind
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeModality
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.filteredSupertypes
object CirClassFactory {
fun create(name: CirName, source: KmClass, typeResolver: CirTypeResolver): CirClass = CirClass.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { CirTypeParameterFactory.create(it, typeResolver) },
visibility = decodeVisibility(source.flags),
modality = decodeModality(source.flags),
kind = decodeClassKind(source.flags),
companion = source.companionObject?.let(CirName::create),
isCompanion = Flag.Class.IS_COMPANION_OBJECT(source.flags),
isData = Flag.Class.IS_DATA(source.flags),
isInline = Flag.Class.IS_INLINE(source.flags),
isInner = Flag.Class.IS_INNER(source.flags),
isExternal = Flag.Class.IS_EXTERNAL(source.flags)
).apply {
setSupertypes(source.filteredSupertypes.compactMap { CirTypeFactory.create(it, typeResolver) })
}
fun createDefaultEnumEntry(
name: CirName,
annotations: List<KmAnnotation>,
enumClassId: CirEntityId,
enumClass: KmClass,
typeResolver: CirTypeResolver
): CirClass = CirClass.create(
annotations = annotations.compactMap { CirAnnotationFactory.create(it, typeResolver) },
name = name,
typeParameters = emptyList(),
visibility = Visibilities.Public,
modality = Modality.FINAL,
kind = ClassKind.ENUM_ENTRY,
companion = null,
isCompanion = false,
isData = false,
isInline = false,
isInner = false,
isExternal = false
).apply {
val enumClassType = CirClassType.createInterned(
classId = enumClassId,
outerType = null,
visibility = decodeVisibility(enumClass.flags),
arguments = emptyList(),
isMarkedNullable = false
)
setSupertypes(listOf(enumClassType))
}
}
@@ -1,63 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.KmAnnotationArgument
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMapIndexed
object CirConstantValueFactory {
fun createSafely(
constantValue: KmAnnotationArgument<*>?,
constantName: CirName? = null,
owner: Any,
): CirConstantValue<*> = createSafely(
constantValue = constantValue,
location = { "${owner::class.java}, $owner" + constantName?.toString()?.let { "[$it]" } }
)
private fun createSafely(
constantValue: KmAnnotationArgument<*>?,
location: () -> String
): CirConstantValue<*> = when (constantValue) {
null -> CirConstantValue.NullValue
is KmAnnotationArgument.StringValue -> CirConstantValue.StringValue(constantValue.value)
is KmAnnotationArgument.CharValue -> CirConstantValue.CharValue(constantValue.value)
is KmAnnotationArgument.ByteValue -> CirConstantValue.ByteValue(constantValue.value)
is KmAnnotationArgument.ShortValue -> CirConstantValue.ShortValue(constantValue.value)
is KmAnnotationArgument.IntValue -> CirConstantValue.IntValue(constantValue.value)
is KmAnnotationArgument.LongValue -> CirConstantValue.LongValue(constantValue.value)
is KmAnnotationArgument.UByteValue -> CirConstantValue.UByteValue(constantValue.value)
is KmAnnotationArgument.UShortValue -> CirConstantValue.UShortValue(constantValue.value)
is KmAnnotationArgument.UIntValue -> CirConstantValue.UIntValue(constantValue.value)
is KmAnnotationArgument.ULongValue -> CirConstantValue.ULongValue(constantValue.value)
is KmAnnotationArgument.FloatValue -> CirConstantValue.FloatValue(constantValue.value)
is KmAnnotationArgument.DoubleValue -> CirConstantValue.DoubleValue(constantValue.value)
is KmAnnotationArgument.BooleanValue -> CirConstantValue.BooleanValue(constantValue.value)
is KmAnnotationArgument.EnumValue -> CirConstantValue.EnumValue(
CirEntityId.create(constantValue.enumClassName),
CirName.create(constantValue.enumEntryName)
)
is KmAnnotationArgument.ArrayValue -> CirConstantValue.ArrayValue(
constantValue.value.compactMapIndexed { index, innerConstantValue ->
createSafely(
constantValue = innerConstantValue,
location = { "${location()}[$index]" }
)
}
)
else -> error("Unsupported annotation argument type: ${constantValue::class.java}, $constantValue")
}
}
@@ -1,16 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.KmType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirExtensionReceiver
object CirExtensionReceiverFactory {
fun create(receiverParameterType: KmType, typeResolver: CirTypeResolver): CirExtensionReceiver = CirExtensionReceiver.create(
annotations = emptyList(), // TODO nowhere to read receiver annotations from, see KT-42490
type = CirTypeFactory.create(receiverParameterType, typeResolver)
)
}
@@ -1,35 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmFunction
import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirContainingClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirFunction
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeCallableKind
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeModality
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirFunctionFactory {
fun create(name: CirName, source: KmFunction, containingClass: CirContainingClass?, typeResolver: CirTypeResolver): CirFunction =
CirFunction.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { CirTypeParameterFactory.create(it, typeResolver) },
visibility = decodeVisibility(source.flags),
modality = decodeModality(source.flags),
containingClass = containingClass,
valueParameters = source.valueParameters.compactMap { CirValueParameterFactory.create(it, typeResolver) },
hasStableParameterNames = !Flag.Function.HAS_NON_STABLE_PARAMETER_NAMES(source.flags),
extensionReceiver = source.receiverParameterType?.let { CirExtensionReceiverFactory.create(it, typeResolver) },
returnType = CirTypeFactory.create(source.returnType, typeResolver),
kind = decodeCallableKind(source.flags),
modifiers = CirFunctionModifiersFactory.create(source),
)
}
@@ -1,21 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmFunction
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirFunctionModifiers
object CirFunctionModifiersFactory {
fun create(source: KmFunction) = CirFunctionModifiers.createInterned(
isOperator = Flag.Function.IS_OPERATOR(source.flags),
isInfix = Flag.Function.IS_INFIX(source.flags),
isInline = Flag.Function.IS_INLINE(source.flags),
isTailrec = Flag.Function.IS_TAILREC(source.flags),
isSuspend = Flag.Function.IS_SUSPEND(source.flags),
isExternal = Flag.Function.IS_EXTERNAL(source.flags)
)
}
@@ -1,52 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmProperty
import kotlinx.metadata.klib.annotations
import kotlinx.metadata.klib.compileTimeValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirContainingClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirProperty
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeCallableKind
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeModality
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirPropertyFactory {
fun create(name: CirName, source: KmProperty, containingClass: CirContainingClass?, typeResolver: CirTypeResolver): CirProperty {
val compileTimeInitializer = if (Flag.Property.HAS_CONSTANT(source.flags)) {
CirConstantValueFactory.createSafely(
constantValue = source.compileTimeValue,
owner = source,
)
} else CirConstantValue.NullValue
return CirProperty.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { CirTypeParameterFactory.create(it, typeResolver) },
visibility = decodeVisibility(source.flags),
modality = decodeModality(source.flags),
containingClass = containingClass,
isExternal = Flag.Property.IS_EXTERNAL(source.flags),
extensionReceiver = source.receiverParameterType?.let { CirExtensionReceiverFactory.create(it, typeResolver) },
returnType = CirTypeFactory.create(source.returnType, typeResolver),
kind = decodeCallableKind(source.flags),
isVar = Flag.Property.IS_VAR(source.flags),
isLateInit = Flag.Property.IS_LATEINIT(source.flags),
isConst = Flag.Property.IS_CONST(source.flags),
isDelegate = Flag.Property.IS_DELEGATED(source.flags),
getter = CirPropertyGetterFactory.create(source, typeResolver),
setter = CirPropertySetterFactory.create(source, typeResolver),
backingFieldAnnotations = emptyList(), // TODO unclear where to read backing/delegate field annotations from, see KT-44625
delegateFieldAnnotations = emptyList(), // TODO unclear where to read backing/delegate field annotations from, see KT-44625
compileTimeInitializer = compileTimeInitializer
)
}
}
@@ -1,33 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmProperty
import kotlinx.metadata.klib.getterAnnotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPropertyGetter
object CirPropertyGetterFactory {
fun create(source: KmProperty, typeResolver: CirTypeResolver): CirPropertyGetter? {
if (!Flag.Property.HAS_GETTER(source.flags))
return null
val getterFlags = source.getterFlags
val isDefault = !Flag.PropertyAccessor.IS_NOT_DEFAULT(getterFlags)
val annotations = CirAnnotationFactory.createAnnotations(getterFlags, typeResolver, source::getterAnnotations)
if (isDefault && annotations.isEmpty())
return CirPropertyGetter.DEFAULT_NO_ANNOTATIONS
return CirPropertyGetter.createInterned(
annotations = annotations,
isDefault = isDefault,
isExternal = Flag.PropertyAccessor.IS_EXTERNAL(getterFlags),
isInline = Flag.PropertyAccessor.IS_INLINE(getterFlags)
)
}
}
@@ -1,33 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmProperty
import kotlinx.metadata.klib.annotations
import kotlinx.metadata.klib.setterAnnotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPropertySetter
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
object CirPropertySetterFactory {
fun create(source: KmProperty, typeResolver: CirTypeResolver): CirPropertySetter? {
if (!Flag.Property.HAS_SETTER(source.flags))
return null
val setterFlags = source.setterFlags
return CirPropertySetter.createInterned(
annotations = CirAnnotationFactory.createAnnotations(setterFlags, typeResolver, source::setterAnnotations),
parameterAnnotations = source.setterParameter?.let { setterParameter ->
CirAnnotationFactory.createAnnotations(setterParameter.flags, typeResolver, setterParameter::annotations)
}.orEmpty(),
visibility = decodeVisibility(setterFlags),
isDefault = !Flag.PropertyAccessor.IS_NOT_DEFAULT(setterFlags),
isExternal = Flag.PropertyAccessor.IS_EXTERNAL(setterFlags),
isInline = Flag.PropertyAccessor.IS_INLINE(setterFlags)
)
}
}
@@ -1,30 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.KmTypeAlias
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassOrTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAlias
import org.jetbrains.kotlin.descriptors.commonizer.cir.unabbreviate
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirTypeAliasFactory {
fun create(name: CirName, source: KmTypeAlias, typeResolver: CirTypeResolver): CirTypeAlias {
val underlyingType = CirTypeFactory.create(source.underlyingType, typeResolver) as CirClassOrTypeAliasType
val expandedType = underlyingType.unabbreviate()
return CirTypeAlias.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { CirTypeParameterFactory.create(it, typeResolver) },
visibility = decodeVisibility(source.flags),
underlyingType = underlyingType,
expandedType = expandedType
)
}
}
@@ -1,86 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.metadata.CirTypeAliasExpander
import org.jetbrains.kotlin.descriptors.commonizer.metadata.CirTypeAliasExpansion
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.types.Variance
object CirTypeFactory {
fun create(source: KmType, typeResolver: CirTypeResolver): CirType {
@Suppress("NAME_SHADOWING")
val source = source.abbreviatedType ?: source
val isMarkedNullable = Flag.Type.IS_NULLABLE(source.flags)
return when (val classifier = source.classifier) {
is KmClassifier.Class -> {
val classId = CirEntityId.create(classifier.name)
val outerType = source.outerType?.let { outerType ->
val outerClassType = create(outerType, typeResolver)
check(outerClassType is CirClassType) { "Outer type of $classId is not a class: $outerClassType" }
outerClassType
}
val clazz: CirProvided.Class = typeResolver.resolveClassifier(classId)
CirClassType.createInterned(
classId = classId,
outerType = outerType,
visibility = clazz.visibility,
arguments = createArguments(source.arguments, typeResolver),
isMarkedNullable = isMarkedNullable
)
}
is KmClassifier.TypeAlias -> {
val typeAliasId = CirEntityId.create(classifier.name)
val arguments = createArguments(source.arguments, typeResolver)
val underlyingType = CirTypeAliasExpander.expand(
CirTypeAliasExpansion.create(typeAliasId, arguments, isMarkedNullable, typeResolver)
)
CirTypeAliasType.createInterned(
typeAliasId = typeAliasId,
underlyingType = underlyingType,
arguments = arguments,
isMarkedNullable = isMarkedNullable
)
}
is KmClassifier.TypeParameter -> {
CirTypeParameterType.createInterned(
index = typeResolver.resolveTypeParameterIndex(classifier.id),
isMarkedNullable = isMarkedNullable
)
}
}
}
@Suppress("NOTHING_TO_INLINE")
inline fun decodeVariance(variance: KmVariance): Variance = when (variance) {
KmVariance.INVARIANT -> Variance.INVARIANT
KmVariance.IN -> Variance.IN_VARIANCE
KmVariance.OUT -> Variance.OUT_VARIANCE
}
@Suppress("NOTHING_TO_INLINE")
private inline fun createArguments(arguments: List<KmTypeProjection>, typeResolver: CirTypeResolver): List<CirTypeProjection> {
return arguments.compactMap { argument ->
val variance = argument.variance ?: return@compactMap CirStarTypeProjection
val argumentType = argument.type ?: return@compactMap CirStarTypeProjection
CirTypeProjectionImpl(
projectionKind = decodeVariance(variance),
type = create(argumentType, typeResolver)
)
}
}
}
@@ -1,26 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmTypeParameter
import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory.decodeVariance
import org.jetbrains.kotlin.descriptors.commonizer.metadata.ALWAYS_HAS_ANNOTATIONS
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.filteredUpperBounds
object CirTypeParameterFactory {
fun create(source: KmTypeParameter, typeResolver: CirTypeResolver): CirTypeParameter = CirTypeParameter.create(
annotations = CirAnnotationFactory.createAnnotations(ALWAYS_HAS_ANNOTATIONS, typeResolver, source::annotations),
name = CirName.create(source.name),
isReified = Flag.TypeParameter.IS_REIFIED(source.flags),
variance = decodeVariance(source.variance),
upperBounds = source.filteredUpperBounds.compactMap { CirTypeFactory.create(it, typeResolver) }
)
}
@@ -1,24 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag
import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
object CirValueParameterFactory {
fun create(source: KmValueParameter, typeResolver: CirTypeResolver) = CirValueParameter.createInterned(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = CirName.create(source.name),
returnType = CirTypeFactory.create(source.type!!, typeResolver),
varargElementType = source.varargElementType?.let { CirTypeFactory.create(it, typeResolver) },
declaresDefaultValue = Flag.ValueParameter.DECLARES_DEFAULT_VALUE(source.flags),
isCrossinline = Flag.ValueParameter.IS_CROSSINLINE(source.flags),
isNoinline = Flag.ValueParameter.IS_NOINLINE(source.flags)
)
}
@@ -15,8 +15,9 @@ import org.jetbrains.kotlin.descriptors.commonizer.LeafCommonizerTarget
import org.jetbrains.kotlin.descriptors.commonizer.ModulesProvider.ModuleInfo
import org.jetbrains.kotlin.descriptors.commonizer.TargetProvider
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.*
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ClassesToProcess.ClassEntry
import org.jetbrains.kotlin.descriptors.commonizer.metadata.CirDeserializers
import org.jetbrains.kotlin.descriptors.commonizer.metadata.CirTypeResolver
import org.jetbrains.kotlin.descriptors.commonizer.metadata.utils.SerializedMetadataLibraryProvider
import org.jetbrains.kotlin.descriptors.commonizer.prettyName
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
@@ -208,7 +209,7 @@ class CirTreeMerger(
val propertyNode: CirPropertyNode = ownerNode.properties.getOrPut(approximationKey) {
buildPropertyNode(storageManager, leafTargetsSize, classifiers, maybeClassOwnerNode?.commonDeclaration)
}
propertyNode.targetDeclarations[context.targetIndex] = CirPropertyFactory.create(
propertyNode.targetDeclarations[context.targetIndex] = CirDeserializers.property(
name = approximationKey.name,
source = property,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex),
@@ -234,7 +235,7 @@ class CirTreeMerger(
val functionNode: CirFunctionNode = ownerNode.functions.getOrPut(approximationKey) {
buildFunctionNode(storageManager, leafTargetsSize, classifiers, maybeClassOwnerNode?.commonDeclaration)
}
functionNode.targetDeclarations[context.targetIndex] = CirFunctionFactory.create(
functionNode.targetDeclarations[context.targetIndex] = CirDeserializers.function(
name = approximationKey.name,
source = function,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex),
@@ -264,13 +265,13 @@ class CirTreeMerger(
clazz = classEntry.clazz
isEnumEntry = Flag.Class.IS_ENUM_ENTRY(clazz.flags)
CirClassFactory.create(className, clazz, context.typeResolver)
CirDeserializers.clazz(className, clazz, context.typeResolver)
}
is ClassEntry.EnumEntry -> {
clazz = null
isEnumEntry = true
CirClassFactory.createDefaultEnumEntry(
CirDeserializers.defaultEnumEntry(
name = className,
annotations = classEntry.annotations,
enumClassId = classEntry.enumClassId,
@@ -313,7 +314,7 @@ class CirTreeMerger(
val constructorNode: CirClassConstructorNode = classNode.constructors.getOrPut(approximationKey) {
buildClassConstructorNode(storageManager, leafTargetsSize, classifiers, classNode.commonDeclaration)
}
constructorNode.targetDeclarations[context.targetIndex] = CirClassConstructorFactory.create(
constructorNode.targetDeclarations[context.targetIndex] = CirDeserializers.constructor(
source = constructor,
containingClass = classNode.targetDeclarations[context.targetIndex]!!,
typeResolver = context.typeResolver
@@ -331,7 +332,7 @@ class CirTreeMerger(
val typeAliasNode: CirTypeAliasNode = packageNode.typeAliases.getOrPut(typeAliasName) {
buildTypeAliasNode(storageManager, leafTargetsSize, classifiers, typeAliasId)
}
typeAliasNode.targetDeclarations[context.targetIndex] = CirTypeAliasFactory.create(
typeAliasNode.targetDeclarations[context.targetIndex] = CirDeserializers.typeAlias(
name = typeAliasName,
source = typeAlias,
typeResolver = context.typeResolver
@@ -10,6 +10,7 @@ import kotlinx.metadata.klib.annotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.descriptors.commonizer.core.Commonizer
import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeParameterResolver
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.computeSignature
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
@@ -78,10 +79,6 @@ data class ConstructorApproximationKey(
.appendHashCode(additionalValueParametersNamesHash)
}
interface TypeParameterResolver {
fun resolveTypeParameter(id: Int): KmTypeParameter?
}
@Suppress("NOTHING_TO_INLINE")
private inline fun List<KmValueParameter>.computeSignatures(typeParameterResolver: TypeParameterResolver): Array<CirTypeSignature> =
if (isEmpty()) emptyArray() else Array(size) { index -> this[index].type?.computeSignature(typeParameterResolver).orEmpty() }
@@ -0,0 +1,428 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.metadata
import gnu.trove.THashMap
import kotlinx.metadata.*
import kotlinx.metadata.klib.annotations
import kotlinx.metadata.klib.compileTimeValue
import kotlinx.metadata.klib.getterAnnotations
import kotlinx.metadata.klib.setterAnnotations
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.types.Variance
object CirDeserializers {
private fun annotations(flags: Flags, typeResolver: CirTypeResolver, annotations: () -> List<KmAnnotation>): List<CirAnnotation> {
return if (!Flag.Common.HAS_ANNOTATIONS(flags))
emptyList()
else
annotations().compactMap { annotation(it, typeResolver) }
}
private fun annotation(source: KmAnnotation, typeResolver: CirTypeResolver): CirAnnotation {
val classId = CirEntityId.create(source.className)
val clazz: CirProvided.Class = typeResolver.resolveClassifier(classId)
val type = CirClassType.createInterned(
classId = classId,
outerType = null, // annotation class can't be inner class
visibility = clazz.visibility,
arguments = clazz.typeParameters.compactMap { typeParameter ->
CirTypeProjectionImpl(
projectionKind = typeParameter.variance,
type = CirTypeParameterType.createInterned(
index = typeParameter.index,
isMarkedNullable = false
)
)
},
isMarkedNullable = false
)
val allValueArguments: Map<String, KmAnnotationArgument<*>> = source.arguments
if (allValueArguments.isEmpty())
return CirAnnotation.createInterned(type = type, constantValueArguments = emptyMap(), annotationValueArguments = emptyMap())
val constantValueArguments: MutableMap<CirName, CirConstantValue<*>> = THashMap(allValueArguments.size)
val annotationValueArguments: MutableMap<CirName, CirAnnotation> = THashMap(allValueArguments.size)
allValueArguments.forEach { (name, constantValue) ->
val cirName = CirName.create(name)
if (constantValue is KmAnnotationArgument.AnnotationValue)
annotationValueArguments[cirName] = annotation(source = constantValue.value, typeResolver)
else
constantValueArguments[cirName] = constantValue(
constantValue = constantValue,
constantName = cirName,
owner = source,
)
}
return CirAnnotation.createInterned(
type = type,
constantValueArguments = constantValueArguments.compact(),
annotationValueArguments = annotationValueArguments.compact()
)
}
private val ALWAYS_HAS_ANNOTATIONS: Flags = flagsOf(Flag.Common.HAS_ANNOTATIONS)
private fun typeParameter(source: KmTypeParameter, typeResolver: CirTypeResolver): CirTypeParameter = CirTypeParameter.create(
annotations = annotations(ALWAYS_HAS_ANNOTATIONS, typeResolver, source::annotations),
name = CirName.create(source.name),
isReified = Flag.TypeParameter.IS_REIFIED(source.flags),
variance = variance(source.variance),
upperBounds = source.filteredUpperBounds.compactMap { type(it, typeResolver) }
)
private fun extensionReceiver(receiverParameterType: KmType, typeResolver: CirTypeResolver): CirExtensionReceiver = CirExtensionReceiver.create(
annotations = emptyList(), // TODO nowhere to read receiver annotations from, see KT-42490
type = type(receiverParameterType, typeResolver)
)
fun property(name: CirName, source: KmProperty, containingClass: CirContainingClass?, typeResolver: CirTypeResolver): CirProperty {
val compileTimeInitializer = if (Flag.Property.HAS_CONSTANT(source.flags)) {
constantValue(
constantValue = source.compileTimeValue,
owner = source,
)
} else CirConstantValue.NullValue
return CirProperty.create(
annotations = annotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { typeParameter(it, typeResolver) },
visibility = visibility(source.flags),
modality = modality(source.flags),
containingClass = containingClass,
isExternal = Flag.Property.IS_EXTERNAL(source.flags),
extensionReceiver = source.receiverParameterType?.let { extensionReceiver(it, typeResolver) },
returnType = type(source.returnType, typeResolver),
kind = callableKind(source.flags),
isVar = Flag.Property.IS_VAR(source.flags),
isLateInit = Flag.Property.IS_LATEINIT(source.flags),
isConst = Flag.Property.IS_CONST(source.flags),
isDelegate = Flag.Property.IS_DELEGATED(source.flags),
getter = propertyGetter(source, typeResolver),
setter = propertySetter(source, typeResolver),
backingFieldAnnotations = emptyList(), // TODO unclear where to read backing/delegate field annotations from, see KT-44625
delegateFieldAnnotations = emptyList(), // TODO unclear where to read backing/delegate field annotations from, see KT-44625
compileTimeInitializer = compileTimeInitializer
)
}
private fun propertyGetter(source: KmProperty, typeResolver: CirTypeResolver): CirPropertyGetter? {
if (!Flag.Property.HAS_GETTER(source.flags))
return null
val getterFlags = source.getterFlags
val isDefault = !Flag.PropertyAccessor.IS_NOT_DEFAULT(getterFlags)
val annotations = annotations(getterFlags, typeResolver, source::getterAnnotations)
if (isDefault && annotations.isEmpty())
return CirPropertyGetter.DEFAULT_NO_ANNOTATIONS
return CirPropertyGetter.createInterned(
annotations = annotations,
isDefault = isDefault,
isExternal = Flag.PropertyAccessor.IS_EXTERNAL(getterFlags),
isInline = Flag.PropertyAccessor.IS_INLINE(getterFlags)
)
}
private fun propertySetter(source: KmProperty, typeResolver: CirTypeResolver): CirPropertySetter? {
if (!Flag.Property.HAS_SETTER(source.flags))
return null
val setterFlags = source.setterFlags
return CirPropertySetter.createInterned(
annotations = annotations(setterFlags, typeResolver, source::setterAnnotations),
parameterAnnotations = source.setterParameter?.let { setterParameter ->
annotations(setterParameter.flags, typeResolver, setterParameter::annotations)
}.orEmpty(),
visibility = visibility(setterFlags),
isDefault = !Flag.PropertyAccessor.IS_NOT_DEFAULT(setterFlags),
isExternal = Flag.PropertyAccessor.IS_EXTERNAL(setterFlags),
isInline = Flag.PropertyAccessor.IS_INLINE(setterFlags)
)
}
@Suppress("NOTHING_TO_INLINE")
private inline fun callableKind(flags: Flags): CallableMemberDescriptor.Kind =
when {
Flag.Property.IS_DECLARATION(flags) /*|| Flag.Function.IS_DECLARATION(flags)*/ -> CallableMemberDescriptor.Kind.DECLARATION
Flag.Property.IS_FAKE_OVERRIDE(flags) /*|| Flag.Function.IS_FAKE_OVERRIDE(flags)*/ -> CallableMemberDescriptor.Kind.FAKE_OVERRIDE
Flag.Property.IS_DELEGATION(flags) /*|| Flag.Function.IS_DELEGATION(flags)*/ -> CallableMemberDescriptor.Kind.DELEGATION
Flag.Property.IS_SYNTHESIZED(flags) /*|| Flag.Function.IS_SYNTHESIZED(flags)*/ -> CallableMemberDescriptor.Kind.SYNTHESIZED
else -> error("Can't decode callable kind from flags: $flags")
}
fun function(name: CirName, source: KmFunction, containingClass: CirContainingClass?, typeResolver: CirTypeResolver): CirFunction =
CirFunction.create(
annotations = annotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { typeParameter(it, typeResolver) },
visibility = visibility(source.flags),
modality = modality(source.flags),
containingClass = containingClass,
valueParameters = source.valueParameters.compactMap { valueParameter(it, typeResolver) },
hasStableParameterNames = !Flag.Function.HAS_NON_STABLE_PARAMETER_NAMES(source.flags),
extensionReceiver = source.receiverParameterType?.let { extensionReceiver(it, typeResolver) },
returnType = type(source.returnType, typeResolver),
kind = callableKind(source.flags),
modifiers = functionModifiers(source),
)
private fun functionModifiers(source: KmFunction): CirFunctionModifiers = CirFunctionModifiers.createInterned(
isOperator = Flag.Function.IS_OPERATOR(source.flags),
isInfix = Flag.Function.IS_INFIX(source.flags),
isInline = Flag.Function.IS_INLINE(source.flags),
isTailrec = Flag.Function.IS_TAILREC(source.flags),
isSuspend = Flag.Function.IS_SUSPEND(source.flags),
isExternal = Flag.Function.IS_EXTERNAL(source.flags)
)
private fun valueParameter(source: KmValueParameter, typeResolver: CirTypeResolver): CirValueParameter =
CirValueParameter.createInterned(
annotations = annotations(source.flags, typeResolver, source::annotations),
name = CirName.create(source.name),
returnType = type(source.type!!, typeResolver),
varargElementType = source.varargElementType?.let { type(it, typeResolver) },
declaresDefaultValue = Flag.ValueParameter.DECLARES_DEFAULT_VALUE(source.flags),
isCrossinline = Flag.ValueParameter.IS_CROSSINLINE(source.flags),
isNoinline = Flag.ValueParameter.IS_NOINLINE(source.flags)
)
private fun constantValue(
constantValue: KmAnnotationArgument<*>?,
constantName: CirName? = null,
owner: Any,
): CirConstantValue<*> = constantValue(
constantValue = constantValue,
location = { "${owner::class.java}, $owner" + constantName?.toString()?.let { "[$it]" } }
)
private fun constantValue(
constantValue: KmAnnotationArgument<*>?,
location: () -> String
): CirConstantValue<*> = when (constantValue) {
null -> CirConstantValue.NullValue
is KmAnnotationArgument.StringValue -> CirConstantValue.StringValue(constantValue.value)
is KmAnnotationArgument.CharValue -> CirConstantValue.CharValue(constantValue.value)
is KmAnnotationArgument.ByteValue -> CirConstantValue.ByteValue(constantValue.value)
is KmAnnotationArgument.ShortValue -> CirConstantValue.ShortValue(constantValue.value)
is KmAnnotationArgument.IntValue -> CirConstantValue.IntValue(constantValue.value)
is KmAnnotationArgument.LongValue -> CirConstantValue.LongValue(constantValue.value)
is KmAnnotationArgument.UByteValue -> CirConstantValue.UByteValue(constantValue.value)
is KmAnnotationArgument.UShortValue -> CirConstantValue.UShortValue(constantValue.value)
is KmAnnotationArgument.UIntValue -> CirConstantValue.UIntValue(constantValue.value)
is KmAnnotationArgument.ULongValue -> CirConstantValue.ULongValue(constantValue.value)
is KmAnnotationArgument.FloatValue -> CirConstantValue.FloatValue(constantValue.value)
is KmAnnotationArgument.DoubleValue -> CirConstantValue.DoubleValue(constantValue.value)
is KmAnnotationArgument.BooleanValue -> CirConstantValue.BooleanValue(constantValue.value)
is KmAnnotationArgument.EnumValue -> CirConstantValue.EnumValue(
CirEntityId.create(constantValue.enumClassName),
CirName.create(constantValue.enumEntryName)
)
is KmAnnotationArgument.ArrayValue -> CirConstantValue.ArrayValue(
constantValue.value.compactMapIndexed { index, innerConstantValue ->
constantValue(
constantValue = innerConstantValue,
location = { "${location()}[$index]" }
)
}
)
else -> error("Unsupported annotation argument type: ${constantValue::class.java}, $constantValue")
}
fun clazz(name: CirName, source: KmClass, typeResolver: CirTypeResolver): CirClass = CirClass.create(
annotations = annotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { typeParameter(it, typeResolver) },
visibility = visibility(source.flags),
modality = modality(source.flags),
kind = classKind(source.flags),
companion = source.companionObject?.let(CirName::create),
isCompanion = Flag.Class.IS_COMPANION_OBJECT(source.flags),
isData = Flag.Class.IS_DATA(source.flags),
isInline = Flag.Class.IS_INLINE(source.flags),
isInner = Flag.Class.IS_INNER(source.flags),
isExternal = Flag.Class.IS_EXTERNAL(source.flags)
).apply {
setSupertypes(source.filteredSupertypes.compactMap { type(it, typeResolver) })
}
fun defaultEnumEntry(
name: CirName,
annotations: List<KmAnnotation>,
enumClassId: CirEntityId,
enumClass: KmClass,
typeResolver: CirTypeResolver
): CirClass = CirClass.create(
annotations = annotations.compactMap { annotation(it, typeResolver) },
name = name,
typeParameters = emptyList(),
visibility = Visibilities.Public,
modality = Modality.FINAL,
kind = ClassKind.ENUM_ENTRY,
companion = null,
isCompanion = false,
isData = false,
isInline = false,
isInner = false,
isExternal = false
).apply {
val enumClassType = CirClassType.createInterned(
classId = enumClassId,
outerType = null,
visibility = visibility(enumClass.flags),
arguments = emptyList(),
isMarkedNullable = false
)
setSupertypes(listOf(enumClassType))
}
@Suppress("NOTHING_TO_INLINE")
private inline fun classKind(flags: Flags): ClassKind =
when {
Flag.Class.IS_CLASS(flags) -> ClassKind.CLASS
Flag.Class.IS_INTERFACE(flags) -> ClassKind.INTERFACE
Flag.Class.IS_ENUM_CLASS(flags) -> ClassKind.ENUM_CLASS
Flag.Class.IS_ENUM_ENTRY(flags) -> ClassKind.ENUM_ENTRY
Flag.Class.IS_ANNOTATION_CLASS(flags) -> ClassKind.ANNOTATION_CLASS
Flag.Class.IS_OBJECT(flags) || Flag.Class.IS_COMPANION_OBJECT(flags) -> ClassKind.OBJECT
else -> error("Can't decode class kind from flags: $flags")
}
fun constructor(source: KmConstructor, containingClass: CirContainingClass, typeResolver: CirTypeResolver): CirClassConstructor =
CirClassConstructor.create(
annotations = annotations(source.flags, typeResolver, source::annotations),
typeParameters = emptyList(), // TODO: nowhere to read constructor type parameters from
visibility = visibility(source.flags),
containingClass = containingClass,
valueParameters = source.valueParameters.compactMap { valueParameter(it, typeResolver) },
hasStableParameterNames = !Flag.Constructor.HAS_NON_STABLE_PARAMETER_NAMES(source.flags),
isPrimary = !Flag.Constructor.IS_SECONDARY(source.flags)
)
fun typeAlias(name: CirName, source: KmTypeAlias, typeResolver: CirTypeResolver): CirTypeAlias {
val underlyingType = type(source.underlyingType, typeResolver) as CirClassOrTypeAliasType
val expandedType = underlyingType.unabbreviate()
return CirTypeAlias.create(
annotations = annotations(source.flags, typeResolver, source::annotations),
name = name,
typeParameters = source.typeParameters.compactMap { typeParameter(it, typeResolver) },
visibility = visibility(source.flags),
underlyingType = underlyingType,
expandedType = expandedType
)
}
private fun type(source: KmType, typeResolver: CirTypeResolver): CirType {
@Suppress("NAME_SHADOWING")
val source = source.abbreviatedType ?: source
val isMarkedNullable = Flag.Type.IS_NULLABLE(source.flags)
return when (val classifier = source.classifier) {
is KmClassifier.Class -> {
val classId = CirEntityId.create(classifier.name)
val outerType = source.outerType?.let { outerType ->
val outerClassType = type(outerType, typeResolver)
check(outerClassType is CirClassType) { "Outer type of $classId is not a class: $outerClassType" }
outerClassType
}
val clazz: CirProvided.Class = typeResolver.resolveClassifier(classId)
CirClassType.createInterned(
classId = classId,
outerType = outerType,
visibility = clazz.visibility,
arguments = arguments(source.arguments, typeResolver),
isMarkedNullable = isMarkedNullable
)
}
is KmClassifier.TypeAlias -> {
val typeAliasId = CirEntityId.create(classifier.name)
val arguments = arguments(source.arguments, typeResolver)
val underlyingType = CirTypeAliasExpander.expand(
CirTypeAliasExpansion.create(typeAliasId, arguments, isMarkedNullable, typeResolver)
)
CirTypeAliasType.createInterned(
typeAliasId = typeAliasId,
underlyingType = underlyingType,
arguments = arguments,
isMarkedNullable = isMarkedNullable
)
}
is KmClassifier.TypeParameter -> {
CirTypeParameterType.createInterned(
index = typeResolver.resolveTypeParameterIndex(classifier.id),
isMarkedNullable = isMarkedNullable
)
}
else -> error("Unexpected classifier type: $classifier")
}
}
@Suppress("NOTHING_TO_INLINE")
private inline fun variance(variance: KmVariance): Variance = when (variance) {
KmVariance.INVARIANT -> Variance.INVARIANT
KmVariance.IN -> Variance.IN_VARIANCE
KmVariance.OUT -> Variance.OUT_VARIANCE
}
@Suppress("NOTHING_TO_INLINE")
private inline fun arguments(arguments: List<KmTypeProjection>, typeResolver: CirTypeResolver): List<CirTypeProjection> {
return arguments.compactMap { argument ->
val variance = argument.variance ?: return@compactMap CirStarTypeProjection
val argumentType = argument.type ?: return@compactMap CirStarTypeProjection
CirTypeProjectionImpl(
projectionKind = variance(variance),
type = type(argumentType, typeResolver)
)
}
}
@Suppress("NOTHING_TO_INLINE")
private inline fun modality(flags: Flags): Modality =
when {
Flag.Common.IS_FINAL(flags) -> Modality.FINAL
Flag.Common.IS_ABSTRACT(flags) -> Modality.ABSTRACT
Flag.Common.IS_OPEN(flags) -> Modality.OPEN
Flag.Common.IS_SEALED(flags) -> Modality.SEALED
else -> error("Can't decode modality from flags: $flags")
}
@Suppress("NOTHING_TO_INLINE")
private inline fun visibility(flags: Flags): Visibility =
when {
Flag.Common.IS_PUBLIC(flags) -> Visibilities.Public
Flag.Common.IS_PROTECTED(flags) -> Visibilities.Protected
Flag.Common.IS_INTERNAL(flags) -> Visibilities.Internal
Flag.Common.IS_PRIVATE(flags) -> Visibilities.Private
else -> error("Can't decode visibility from flags: $flags")
}
}
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.metadata
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeResolver
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMapIndexed
import org.jetbrains.kotlin.types.Variance
@@ -3,14 +3,13 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
package org.jetbrains.kotlin.descriptors.commonizer.metadata
import gnu.trove.TIntObjectHashMap
import kotlinx.metadata.KmTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvidedClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.TypeParameterResolver
typealias TypeParameterId = Int
typealias TypeParameterIndex = Int
@@ -0,0 +1,12 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.metadata
import kotlinx.metadata.KmTypeParameter
interface TypeParameterResolver {
fun resolveTypeParameter(id: Int): KmTypeParameter?
}
@@ -8,11 +8,13 @@ package org.jetbrains.kotlin.descriptors.commonizer.metadata
import kotlinx.metadata.Flag
import kotlinx.metadata.Flags
import kotlinx.metadata.flagsOf
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
internal const val NO_FLAGS: Flags = 0
internal val ALWAYS_HAS_ANNOTATIONS: Flags = flagsOf(Flag.Common.HAS_ANNOTATIONS)
internal fun CirFunction.functionFlags(isExpect: Boolean): Flags =
flagsOfNotNull(
@@ -98,48 +100,6 @@ internal fun CirTypeAlias.typeAliasFlags(): Flags =
visibilityFlag
)
@Suppress("NOTHING_TO_INLINE")
internal inline fun decodeVisibility(flags: Flags): Visibility =
when {
Flag.Common.IS_PUBLIC(flags) -> Visibilities.Public
Flag.Common.IS_PROTECTED(flags) -> Visibilities.Protected
Flag.Common.IS_INTERNAL(flags) -> Visibilities.Internal
Flag.Common.IS_PRIVATE(flags) -> Visibilities.Private
else -> error("Can't decode visibility from flags: $flags")
}
@Suppress("NOTHING_TO_INLINE")
internal inline fun decodeModality(flags: Flags): Modality =
when {
Flag.Common.IS_FINAL(flags) -> Modality.FINAL
Flag.Common.IS_ABSTRACT(flags) -> Modality.ABSTRACT
Flag.Common.IS_OPEN(flags) -> Modality.OPEN
Flag.Common.IS_SEALED(flags) -> Modality.SEALED
else -> error("Can't decode modality from flags: $flags")
}
@Suppress("NOTHING_TO_INLINE")
internal inline fun decodeCallableKind(flags: Flags): CallableMemberDescriptor.Kind =
when {
Flag.Property.IS_DECLARATION(flags) /*|| Flag.Function.IS_DECLARATION(flags)*/ -> CallableMemberDescriptor.Kind.DECLARATION
Flag.Property.IS_FAKE_OVERRIDE(flags) /*|| Flag.Function.IS_FAKE_OVERRIDE(flags)*/ -> CallableMemberDescriptor.Kind.FAKE_OVERRIDE
Flag.Property.IS_DELEGATION(flags) /*|| Flag.Function.IS_DELEGATION(flags)*/ -> CallableMemberDescriptor.Kind.DELEGATION
Flag.Property.IS_SYNTHESIZED(flags) /*|| Flag.Function.IS_SYNTHESIZED(flags)*/ -> CallableMemberDescriptor.Kind.SYNTHESIZED
else -> error("Can't decode callable kind from flags: $flags")
}
@Suppress("NOTHING_TO_INLINE")
internal inline fun decodeClassKind(flags: Flags): ClassKind =
when {
Flag.Class.IS_CLASS(flags) -> ClassKind.CLASS
Flag.Class.IS_INTERFACE(flags) -> ClassKind.INTERFACE
Flag.Class.IS_ENUM_CLASS(flags) -> ClassKind.ENUM_CLASS
Flag.Class.IS_ENUM_ENTRY(flags) -> ClassKind.ENUM_ENTRY
Flag.Class.IS_ANNOTATION_CLASS(flags) -> ClassKind.ANNOTATION_CLASS
Flag.Class.IS_OBJECT(flags) || Flag.Class.IS_COMPANION_OBJECT(flags) -> ClassKind.OBJECT
else -> error("Can't decode class kind from flags: $flags")
}
private inline val CirHasAnnotations.hasAnnotationsFlag: Flag?
get() = if (annotations.isNotEmpty()) Flag.Common.HAS_ANNOTATIONS else null
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.utils
import gnu.trove.TIntHashSet
import kotlinx.metadata.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.TypeParameterResolver
import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeParameterResolver
internal inline val KmTypeParameter.filteredUpperBounds: List<KmType>
get() = upperBounds.takeUnless { it.singleOrNull()?.isNullableAny == true } ?: emptyList()
@@ -13,8 +13,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.*
import org.jetbrains.kotlin.descriptors.commonizer.ModulesProvider.ModuleInfo
import org.jetbrains.kotlin.descriptors.commonizer.ResultsConsumer.ModuleResult
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirClassFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.konan.NativeSensitiveManifestData
import org.jetbrains.kotlin.descriptors.commonizer.konan.TargetedNativeManifestDataProvider
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*