提交 4bab505c 编写于 作者: D Dmitriy Dolovov

[Commonizer] Introduce CIR entities for representing various flavors of names

- CirName - simple name
- CirPackageName - fully-qualified name of the package
- CirEntityName - fully-qualified name of some entity, ex: Class, TypeAlias
上级 f8c5244a
......@@ -5,10 +5,8 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
import org.jetbrains.kotlin.name.Name
interface CirAnnotation {
val type: CirClassType
val constantValueArguments: Map<Name, CirConstantValue<*>>
val annotationValueArguments: Map<Name, CirAnnotation>
val constantValueArguments: Map<CirName, CirConstantValue<*>>
val annotationValueArguments: Map<CirName, CirAnnotation>
}
......@@ -5,10 +5,8 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
import org.jetbrains.kotlin.name.Name
interface CirClass : CirClassifier, CirContainingClass {
var companion: Name? // null means no companion object
var companion: CirName? // null means no companion object
val isCompanion: Boolean
val isInline: Boolean
val isInner: Boolean
......
......@@ -5,9 +5,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
sealed class CirConstantValue<out T> {
abstract val value: T
......@@ -28,8 +25,8 @@ sealed class CirConstantValue<out T> {
data class DoubleValue(override val value: Double) : CirConstantValue<Double>()
data class BooleanValue(override val value: Boolean) : CirConstantValue<Boolean>()
data class EnumValue(val enumClassId: ClassId, val enumEntryName: Name) : CirConstantValue<String>() {
override val value: String = "${enumClassId.asString()}.${enumEntryName.asString()}"
data class EnumValue(val enumClassId: CirEntityId, val enumEntryName: CirName) : CirConstantValue<String>() {
override val value: String = "${enumClassId}.${enumEntryName}"
}
data class ArrayValue(override val value: List<CirConstantValue<*>>) : CirConstantValue<List<CirConstantValue<*>>>()
......
......@@ -6,8 +6,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
/**
* An intermediate representation of [DeclarationDescriptor]s for commonization purposes.
......@@ -17,7 +15,7 @@ import org.jetbrains.kotlin.name.Name
* - [CirTypeAlias] - [TypeAliasDescriptor]
* - [CirFunction] - [SimpleFunctionDescriptor]
* - [CirProperty] - [PropertyDescriptor]
* - [CirPackage] - union of multiple [PackageFragmentDescriptor]s with the same [FqName] contributed by commonized [ModuleDescriptor]s
* - [CirPackage] - union of multiple [PackageFragmentDescriptor]s with the same FQ name contributed by commonized [ModuleDescriptor]s
* - [CirModule] - [ModuleDescriptor]
* - [CirRoot] - the root of the whole Commonizer IR tree
*/
......@@ -28,11 +26,7 @@ interface CirHasAnnotations {
}
interface CirHasName {
val name: Name
}
interface CirHasFqName {
val fqName: FqName
val name: CirName
}
interface CirHasVisibility {
......
/*
* 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
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName.Companion.create
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName.Companion.create
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
/**
* A representation of a simple name. Examples:
* 1) name of class without package and outer class name (if this is nested class).
* 2) name of class member such as property or function.
* 3) name of type parameter, name of value parameter.
*
* New instances are created via [create] method which encapsulates interning to avoid duplicated instances.
*/
class CirName private constructor(val name: String) {
override fun equals(other: Any?): Boolean = other is CirName && (other === this || other.name == name)
override fun hashCode(): Int = hashCode(name)
override fun toString(): String = name
fun toStrippedString(): String = name.removeSurrounding("<", ">")
companion object {
fun create(name: String): CirName = interner.intern(CirName(name))
fun create(name: Name): CirName = create(name.asString())
private val interner = Interner<CirName>()
}
}
/**
* A representation of a fully-qualified package name.
*
* New instances are created via [create] method which encapsulates interning to avoid duplicated instances.
*/
class CirPackageName private constructor(val segments: Array<String>) {
override fun equals(other: Any?): Boolean = other is CirPackageName && (other === this || other.segments.contentEquals(segments))
override fun hashCode(): Int = hashCode(segments)
override fun toString(): String = segments.joinToString(".")
fun toMetadataString(): String = segments.joinToString("/")
fun isRoot(): Boolean = segments.isEmpty()
fun startsWith(other: CirPackageName): Boolean {
return when {
other.isRoot() -> true
other.segments.size > segments.size -> false
else -> {
for (i in other.segments.indices) {
if (segments[i] != other.segments[i]) return false
}
true
}
}
}
companion object {
val ROOT: CirPackageName = CirPackageName(emptyArray())
fun create(packageFqName: String): CirPackageName = create(splitComplexNameToArray(packageFqName, ".") { it })
fun create(packageFqName: FqName): CirPackageName = if (packageFqName.isRoot) ROOT else create(packageFqName.asString())
fun create(segments: Array<String>): CirPackageName = if (segments.isEmpty()) ROOT else interner.intern(CirPackageName(segments))
private val interner = Interner<CirPackageName>()
init {
interner.intern(ROOT)
}
}
}
/**
* A representation of a fully-qualified classifier ID which includes [CirPackageName] and few [CirName] elements
* to uniquely address any class or type alias. Outer classes and type aliases always have single element
* in [relativeNameSegments]. While nested classes have more than one element in [relativeNameSegments] (the amount
* of elements depends on the nesting depth).
*
* New instances are created via [create] method which encapsulates interning to avoid duplicated instances.
*/
class CirEntityId private constructor(val packageName: CirPackageName, val relativeNameSegments: Array<CirName>) {
override fun equals(other: Any?): Boolean = when {
other === this -> true
other is CirEntityId -> other.packageName == packageName && other.relativeNameSegments.contentEquals(relativeNameSegments)
else -> false
}
override fun hashCode(): Int = hashCode(packageName).appendHashCode(relativeNameSegments)
override fun toString(): String = buildString {
packageName.segments.joinTo(this, "/")
append('/')
relativeNameSegments.joinTo(this, ".")
}
fun createNestedEntityId(entityName: CirName): CirEntityId = create(packageName, relativeNameSegments + entityName)
val isNestedEntity: Boolean get() = relativeNameSegments.size > 1
companion object {
fun create(entityId: String): CirEntityId {
val rawPackageName: String
val rawRelativeName: String
when (val index = entityId.lastIndexOf('/')) {
-1 -> {
rawPackageName = ""
rawRelativeName = entityId
}
else -> {
rawPackageName = entityId.substring(0, index)
rawRelativeName = entityId.substring(index + 1)
}
}
val packageName = CirPackageName.create(splitComplexNameToArray(rawPackageName, "/") { it })
val relativeNameSegments = splitComplexNameToArray(rawRelativeName, ".", CirName::create)
return create(packageName, relativeNameSegments)
}
fun create(classifierId: ClassId): CirEntityId {
val packageName = CirPackageName.create(classifierId.packageFqName)
val relativeNameSegments = splitComplexNameToArray(classifierId.relativeClassName.asString(), ".", CirName::create)
return create(packageName, relativeNameSegments)
}
fun create(packageName: CirPackageName, relativeName: CirName): CirEntityId = create(packageName, arrayOf(relativeName))
fun create(packageName: CirPackageName, relativeNameSegments: Array<CirName>): CirEntityId =
interner.intern(CirEntityId(packageName, relativeNameSegments))
private val interner = Interner<CirEntityId>()
}
}
private inline fun <reified T> splitComplexNameToArray(complexName: String, delimiter: String, transform: (String) -> T): Array<T> {
if (complexName.isEmpty()) return emptyArray()
val segments = complexName.split(delimiter)
return Array(segments.size) { index -> transform(segments[index]) }
}
......@@ -5,4 +5,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
interface CirPackage : CirDeclaration, CirHasFqName
interface CirPackage : CirDeclaration {
val packageName: CirPackageName
}
......@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.types.AbbreviatedType
import org.jetbrains.kotlin.types.Variance
......@@ -58,13 +57,13 @@ data class CirTypeParameterType(
}
sealed class CirClassOrTypeAliasType : CirSimpleType() {
abstract val classifierId: ClassId
abstract val classifierId: CirEntityId
abstract val arguments: List<CirTypeProjection>
override fun appendDescriptionTo(builder: StringBuilder) = appendDescriptionTo(builder, shortNameOnly = false)
protected open fun appendDescriptionTo(builder: StringBuilder, shortNameOnly: Boolean) {
builder.append(if (shortNameOnly) classifierId.relativeClassName.shortName().asString() else classifierId.asString())
builder.append(if (shortNameOnly) classifierId.relativeNameSegments.last() else classifierId)
if (arguments.isNotEmpty()) arguments.joinTo(builder, prefix = "<", postfix = ">")
super.appendDescriptionTo(builder)
}
......
......@@ -10,10 +10,10 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirAnnotationImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.compact
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.constants.AnnotationValue
import org.jetbrains.kotlin.resolve.constants.ConstantValue
......@@ -28,16 +28,17 @@ object CirAnnotationFactory {
if (allValueArguments.isEmpty())
return create(type = type, constantValueArguments = emptyMap(), annotationValueArguments = emptyMap())
val constantValueArguments: MutableMap<Name, CirConstantValue<*>> = THashMap(allValueArguments.size)
val annotationValueArguments: MutableMap<Name, CirAnnotation> = THashMap(allValueArguments.size)
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 AnnotationValue)
annotationValueArguments[name.intern()] = create(source = constantValue.value)
annotationValueArguments[cirName] = create(source = constantValue.value)
else
constantValueArguments[name.intern()] = CirConstantValueFactory.createSafely(
constantValueArguments[cirName] = CirConstantValueFactory.createSafely(
constantValue = constantValue,
constantName = name,
constantName = cirName,
owner = source,
)
}
......@@ -51,8 +52,8 @@ object CirAnnotationFactory {
fun create(
type: CirClassType,
constantValueArguments: Map<Name, CirConstantValue<*>>,
annotationValueArguments: Map<Name, CirAnnotation>
constantValueArguments: Map<CirName, CirConstantValue<*>>,
annotationValueArguments: Map<CirName, CirAnnotation>
): CirAnnotation {
return interner.intern(
CirAnnotationImpl(
......
......@@ -11,22 +11,21 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirClassImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.isInlineClass
object CirClassFactory {
fun create(source: ClassDescriptor): CirClass = create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
typeParameters = source.declaredTypeParameters.compactMap(CirTypeParameterFactory::create),
visibility = source.visibility,
modality = source.modality,
kind = source.kind,
companion = source.companionObjectDescriptor?.name?.intern(),
companion = source.companionObjectDescriptor?.name?.let(CirName::create),
isCompanion = source.isCompanionObject,
isData = source.isData,
isInline = source.isInlineClass(),
......@@ -39,12 +38,12 @@ object CirClassFactory {
@Suppress("NOTHING_TO_INLINE")
inline fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
typeParameters: List<CirTypeParameter>,
visibility: DescriptorVisibility,
modality: Modality,
kind: ClassKind,
companion: Name?,
companion: CirName?,
isCompanion: Boolean,
isData: Boolean,
isInline: Boolean,
......
......@@ -6,19 +6,19 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
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
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.constants.*
object CirConstantValueFactory {
fun createSafely(
constantValue: ConstantValue<*>,
constantName: Name? = null,
constantName: CirName? = null,
owner: Any,
): CirConstantValue<*> = createSafely(
constantValue = constantValue,
location = { "${owner::class.java}, $owner" + constantName?.asString()?.let { "[$it]" } }
location = { "${owner::class.java}, $owner" + constantName?.toString()?.let { "[$it]" } }
)
private fun createSafely(
......@@ -42,7 +42,10 @@ object CirConstantValueFactory {
is DoubleValue -> CirConstantValue.DoubleValue(constantValue.value)
is BooleanValue -> CirConstantValue.BooleanValue(constantValue.value)
is EnumValue -> CirConstantValue.EnumValue(constantValue.enumClassId.intern(), constantValue.enumEntryName.intern())
is EnumValue -> CirConstantValue.EnumValue(
CirEntityId.create(constantValue.enumClassId),
CirName.create(constantValue.enumEntryName)
)
is NullValue -> CirConstantValue.NullValue
is ArrayValue -> CirConstantValue.ArrayValue(
......
......@@ -9,13 +9,11 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirFunctionImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
object CirFunctionFactory {
fun create(source: SimpleFunctionDescriptor, containingClass: CirContainingClass?): CirFunction = create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
typeParameters = source.typeParameters.compactMap(CirTypeParameterFactory::create),
visibility = source.visibility,
modality = source.modality,
......@@ -31,7 +29,7 @@ object CirFunctionFactory {
@Suppress("NOTHING_TO_INLINE")
inline fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
typeParameters: List<CirTypeParameter>,
visibility: DescriptorVisibility,
modality: Modality,
......
......@@ -5,14 +5,9 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirModule
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirModuleImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
object CirModuleFactory {
fun create(source: ModuleDescriptor): CirModule = create(source.name.intern())
fun create(name: Name) = CirModuleImpl(name)
fun create(name: CirName) = CirModuleImpl(name)
}
......@@ -6,9 +6,9 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackage
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirPackageImpl
import org.jetbrains.kotlin.name.FqName
object CirPackageFactory {
fun create(fqName: FqName): CirPackage = CirPackageImpl(fqName)
fun create(packageName: CirPackageName): CirPackage = CirPackageImpl(packageName)
}
......@@ -12,8 +12,6 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirPropertyImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
object CirPropertyFactory {
fun create(source: PropertyDescriptor, containingClass: CirContainingClass?): CirProperty {
......@@ -26,7 +24,7 @@ object CirPropertyFactory {
return create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
typeParameters = source.typeParameters.compactMap(CirTypeParameterFactory::create),
visibility = source.visibility,
modality = source.modality,
......@@ -50,7 +48,7 @@ object CirPropertyFactory {
@Suppress("NOTHING_TO_INLINE")
inline fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
typeParameters: List<CirTypeParameter>,
visibility: DescriptorVisibility,
modality: Modality,
......
......@@ -10,13 +10,11 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeAliasImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
object CirTypeAliasFactory {
fun create(source: TypeAliasDescriptor): CirTypeAlias = create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
typeParameters = source.declaredTypeParameters.compactMap(CirTypeParameterFactory::create),
visibility = source.visibility,
underlyingType = CirTypeFactory.create(source.underlyingType, useAbbreviation = true) as CirClassOrTypeAliasType,
......@@ -26,7 +24,7 @@ object CirTypeAliasFactory {
@Suppress("NOTHING_TO_INLINE")
inline fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
typeParameters: List<CirTypeParameter>,
visibility: DescriptorVisibility,
underlyingType: CirClassOrTypeAliasType,
......
......@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirClassTypeImpl
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeAliasTypeImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.types.*
object CirTypeFactory {
......@@ -42,7 +41,7 @@ object CirTypeFactory {
when (val classifierDescriptor = abbreviation.declarationDescriptor) {
is TypeAliasDescriptor -> {
return createTypeAliasType(
typeAliasId = classifierDescriptor.internedClassId,
typeAliasId = classifierDescriptor.classifierId,
underlyingType = create(extractExpandedType(source), useAbbreviation = true) as CirClassOrTypeAliasType,
arguments = createArguments(abbreviation.arguments, useAbbreviation = true),
isMarkedNullable = abbreviation.isMarkedNullable
......@@ -70,7 +69,7 @@ object CirTypeFactory {
val cirExpandedTypeWithProperNullability = if (source.isMarkedNullable) makeNullable(cirExpandedType) else cirExpandedType
createTypeAliasType(
typeAliasId = classifierDescriptor.internedClassId,
typeAliasId = classifierDescriptor.classifierId,
underlyingType = cirExpandedTypeWithProperNullability,
arguments = createArguments(source.arguments, useAbbreviation = true),
isMarkedNullable = source.isMarkedNullable
......@@ -82,7 +81,7 @@ object CirTypeFactory {
}
fun createClassType(
classId: ClassId,
classId: CirEntityId,
outerType: CirClassType?,
visibility: DescriptorVisibility,
arguments: List<CirTypeProjection>,
......@@ -100,7 +99,7 @@ object CirTypeFactory {
}
fun createTypeAliasType(
typeAliasId: ClassId,
typeAliasId: CirEntityId,
underlyingType: CirClassOrTypeAliasType,
arguments: List<CirTypeProjection>,
isMarkedNullable: Boolean
......@@ -170,7 +169,7 @@ object CirTypeFactory {
}
return createClassType(
classId = classDescriptor.internedClassId,
classId = classDescriptor.classifierId,
outerType = outerType,
visibility = classDescriptor.visibility,
arguments = remainingArguments,
......
......@@ -7,12 +7,11 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeParameterImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.isNullableAny
......@@ -23,7 +22,7 @@ object CirTypeParameterFactory {
return create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
isReified = source.isReified,
variance = source.variance,
upperBounds = filteredUpperBounds.compactMap(CirTypeFactory::create)
......@@ -33,7 +32,7 @@ object CirTypeParameterFactory {
@Suppress("NOTHING_TO_INLINE")
inline fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
isReified: Boolean,
variance: Variance,
upperBounds: List<CirType>
......
......@@ -7,20 +7,19 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirValueParameterImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.name.Name
object CirValueParameterFactory {
private val interner = Interner<CirValueParameter>()
fun create(source: ValueParameterDescriptor): CirValueParameter = create(
annotations = source.annotations.compactMap(CirAnnotationFactory::create),
name = source.name.intern(),
name = CirName.create(source.name),
returnType = CirTypeFactory.create(source.returnType!!),
varargElementType = source.varargElementType?.let(CirTypeFactory::create),
declaresDefaultValue = source.declaresDefaultValue(),
......@@ -30,7 +29,7 @@ object CirValueParameterFactory {
fun create(
annotations: List<CirAnnotation>,
name: Name,
name: CirName,
returnType: CirType,
varargElementType: CirType?,
declaresDefaultValue: Boolean,
......
......@@ -8,14 +8,14 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.name.Name
data class CirAnnotationImpl(
override val type: CirClassType,
override val constantValueArguments: Map<Name, CirConstantValue<*>>,
override val annotationValueArguments: Map<Name, CirAnnotation>
override val constantValueArguments: Map<CirName, CirConstantValue<*>>,
override val annotationValueArguments: Map<CirName, CirAnnotation>
) : CirAnnotation {
// See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode
private var cachedHashCode = 0
......
......@@ -8,20 +8,16 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
data class CirClassImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val typeParameters: List<CirTypeParameter>,
override val visibility: DescriptorVisibility,
override val modality: Modality,
override val kind: ClassKind,
override var companion: Name?,
override var companion: CirName?,
override val isCompanion: Boolean,
override val isData: Boolean,
override val isInline: Boolean,
......
......@@ -7,13 +7,13 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.name.ClassId
data class CirClassTypeImpl(
override val classifierId: ClassId,
override val classifierId: CirEntityId,
override val outerType: CirClassType?,
override val visibility: DescriptorVisibility, // visibility of the class descriptor
override val arguments: List<CirTypeProjection>,
......
......@@ -9,11 +9,10 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.name.Name
data class CirFunctionImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val typeParameters: List<CirTypeParameter>,
override val visibility: DescriptorVisibility,
override val modality: Modality,
......
......@@ -6,8 +6,8 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirModule
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
data class CirModuleImpl(
override val name: Name
override val name: CirName
) : CirModule
......@@ -6,8 +6,8 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackage
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
data class CirPackageImpl(
override val fqName: FqName
override val packageName: CirPackageName
) : CirPackage
......@@ -9,11 +9,10 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.name.Name
data class CirPropertyImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val typeParameters: List<CirTypeParameter>,
override val visibility: DescriptorVisibility,
override val modality: Modality,
......
......@@ -7,11 +7,10 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.name.Name
data class CirTypeAliasImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val typeParameters: List<CirTypeParameter>,
override val visibility: DescriptorVisibility,
override val underlyingType: CirClassOrTypeAliasType,
......
......@@ -6,14 +6,14 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassOrTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.name.ClassId
data class CirTypeAliasTypeImpl(
override val classifierId: ClassId,
override val classifierId: CirEntityId,
override val underlyingType: CirClassOrTypeAliasType,
override val arguments: List<CirTypeProjection>,
override val isMarkedNullable: Boolean
......
......@@ -6,14 +6,14 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
data class CirTypeParameterImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val isReified: Boolean,
override val variance: Variance,
override val upperBounds: List<CirType>
......
......@@ -6,15 +6,15 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.name.Name
data class CirValueParameterImpl(
override val annotations: List<CirAnnotation>,
override val name: Name,
override val name: CirName,
override val returnType: CirType,
override val varargElementType: CirType?,
override val declaresDefaultValue: Boolean,
......
......@@ -5,11 +5,7 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassifier
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirRecursionMarker
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
object CirClassRecursionMarker : CirClass, CirRecursionMarker {
override val annotations get() = unsupported()
......@@ -18,7 +14,7 @@ object CirClassRecursionMarker : CirClass, CirRecursionMarker {
override val visibility get() = unsupported()
override val modality get() = unsupported()
override val kind get() = unsupported()
override var companion: Name?
override var companion: CirName?
get() = unsupported()
set(_) = unsupported()
override val isCompanion get() = unsupported()
......
......@@ -9,13 +9,13 @@ import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.DELEGATION
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirFunctionOrProperty
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.name.Name
abstract class AbstractFunctionOrPropertyCommonizer<T : CirFunctionOrProperty>(
classifiers: CirKnownClassifiers
) : AbstractStandardCommonizer<T, T>() {
protected lateinit var name: Name
protected lateinit var name: CirName
protected val modality = ModalityCommonizer()
protected val visibility = VisibilityCommonizer.lowering()
protected val extensionReceiver = ExtensionReceiverCommonizer(classifiers)
......
......@@ -9,13 +9,12 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
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.cir.factory.CirAnnotationFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.core.AnnotationsCommonizer.Companion.FALLBACK_MESSAGE
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import kotlin.DeprecationLevel.WARNING
/**
......@@ -56,20 +55,22 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
val level: DeprecationLevel = level ?: failInEmptyState()
val messageValue: StringValue = message.toDeprecationMessageValue()
val constantValueArguments: Map<Name, CirConstantValue<*>> = if (level == WARNING) {
// don't populate with the default level value
compactMapOf(PROPERTY_NAME_MESSAGE, messageValue)
} else
compactMapOf(
PROPERTY_NAME_MESSAGE, messageValue,
PROPERTY_NAME_LEVEL, level.toDeprecationLevelValue()
)
val annotationValueArguments: Map<Name, CirAnnotation> = if (replaceWithExpression.isEmpty() && replaceWithImports.isEmpty()) {
// don't populate with empty (default) ReplaceWith
emptyMap()
} else
compactMapOf(PROPERTY_NAME_REPLACE_WITH, replaceWithExpression.toReplaceWithValue(replaceWithImports))
val constantValueArguments: Map<CirName, CirConstantValue<*>> =
if (level == WARNING) {
// don't populate with the default level value
compactMapOf(PROPERTY_NAME_MESSAGE, messageValue)
} else
compactMapOf(
PROPERTY_NAME_MESSAGE, messageValue,
PROPERTY_NAME_LEVEL, level.toDeprecationLevelValue()
)
val annotationValueArguments: Map<CirName, CirAnnotation> =
if (replaceWithExpression.isEmpty() && replaceWithImports.isEmpty()) {
// don't populate with empty (default) ReplaceWith
emptyMap()
} else
compactMapOf(PROPERTY_NAME_REPLACE_WITH, replaceWithExpression.toReplaceWithValue(replaceWithImports))
return CirAnnotationFactory.create(
type = DEPRECATED_ANNOTATION_TYPE,
......@@ -128,12 +129,12 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
@Suppress("NOTHING_TO_INLINE")
companion object {
private val PROPERTY_NAME_MESSAGE = Name.identifier(Deprecated::message.name).intern()
private val PROPERTY_NAME_REPLACE_WITH = Name.identifier(Deprecated::replaceWith.name).intern()
private val PROPERTY_NAME_LEVEL = Name.identifier(Deprecated::level.name).intern()
private val PROPERTY_NAME_MESSAGE = CirName.create(Deprecated::message.name)
private val PROPERTY_NAME_REPLACE_WITH = CirName.create(Deprecated::replaceWith.name)
private val PROPERTY_NAME_LEVEL = CirName.create(Deprecated::level.name)
private val PROPERTY_NAME_EXPRESSION = Name.identifier(ReplaceWith::expression.name).intern()
private val PROPERTY_NAME_IMPORTS = Name.identifier(ReplaceWith::imports.name).intern()
private val PROPERTY_NAME_EXPRESSION = CirName.create(ReplaceWith::expression.name)
private val PROPERTY_NAME_IMPORTS = CirName.create(ReplaceWith::imports.name)
// Optimization: Keep most frequently used message constants.
private val FREQUENTLY_USED_MESSAGE_VALUES: Map<String, StringValue> = listOf(
......@@ -143,16 +144,16 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
private val FALLBACK_MESSAGE_VALUE = StringValue(FALLBACK_MESSAGE)
private val DEPRECATED_ANNOTATION_TYPE = buildAnnotationType(DEPRECATED_ANNOTATION_CLASS_ID)
private val REPLACE_WITH_ANNOTATION_TYPE = buildAnnotationType(internedClassId(FqName(ReplaceWith::class.java.name)))
private val REPLACE_WITH_ANNOTATION_TYPE = buildAnnotationType(CirEntityId.create("kotlin/ReplaceWith"))
private val DEPRECATION_LEVEL_CLASS_ID = internedClassId(FqName(DeprecationLevel::class.java.name))
private val DEPRECATION_LEVEL_CLASS_ID = CirEntityId.create("kotlin/DeprecationLevel")
// Optimization: Keep DeprecationLevel enum constants.
private val DEPRECATION_LEVEL_ENUM_ENTRY_VALUES: Map<String, EnumValue> = DeprecationLevel.values().associate {
it.name to EnumValue(DEPRECATION_LEVEL_CLASS_ID, Name.identifier(it.name).intern())
it.name to EnumValue(DEPRECATION_LEVEL_CLASS_ID, CirName.create(it.name))
}
private fun buildAnnotationType(classId: ClassId) = CirTypeFactory.createClassType(
private fun buildAnnotationType(classId: CirEntityId) = CirTypeFactory.createClassType(
classId = classId,
outerType = null,
visibility = DescriptorVisibilities.PUBLIC,
......@@ -188,16 +189,16 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
private fun String.toReplaceWithValue(imports: List<String>): CirAnnotation =
createReplaceWithAnnotation(this, imports)
private inline fun Map<Name, CirConstantValue<*>>.getString(name: Name): String? =
private inline fun Map<CirName, CirConstantValue<*>>.getString(name: CirName): String? =
(this[name] as? StringValue)?.value
private inline fun Map<Name, CirConstantValue<*>>.getEnumEntryName(name: Name): String? =
(this[name] as? EnumValue)?.enumEntryName?.asString()
private inline fun Map<CirName, CirConstantValue<*>>.getEnumEntryName(name: CirName): String? =
(this[name] as? EnumValue)?.enumEntryName?.name
private inline fun Map<Name, CirAnnotation>.getAnnotation(name: Name): CirAnnotation? =
private inline fun Map<CirName, CirAnnotation>.getAnnotation(name: CirName): CirAnnotation? =
this[name]
private inline fun Map<Name, CirConstantValue<*>>.getStringArray(name: Name): List<String>? {
private inline fun Map<CirName, CirConstantValue<*>>.getStringArray(name: CirName): List<String>? {
val elements: List<CirConstantValue<*>> = (this[name] as? ArrayValue)?.value ?: return null
if (elements.isEmpty()) return emptyList()
......
......@@ -8,15 +8,14 @@ package org.jetbrains.kotlin.descriptors.commonizer.core
import com.intellij.util.containers.FactoryMap
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirCallableMemberWithParameters
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirHasAnnotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirValueParameterFactory
import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.doNothing
import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.patchCallables
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMapIndexed
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.descriptors.commonizer.utils.isObjCInteropCallableAnnotation
import org.jetbrains.kotlin.name.Name
class CallableValueParametersCommonizer(
classifiers: CirKnownClassifiers
......@@ -40,7 +39,7 @@ class CallableValueParametersCommonizer(
companion object {
fun doNothing(): () -> Unit = {}
fun List<CallableToPatch>.patchCallables(generated: Boolean, newNames: List<Name>): () -> Unit {
fun List<CallableToPatch>.patchCallables(generated: Boolean, newNames: List<CirName>): () -> Unit {
val callablesToPatch = filter { it.originalNames is ValueParameterNames.Generated == generated }
.takeIf { it.isNotEmpty() }
?: return doNothing()
......@@ -72,10 +71,10 @@ class CallableValueParametersCommonizer(
private sealed class ValueParameterNames {
object Generated : ValueParameterNames()
data class Real(val names: List<Name>) : ValueParameterNames()
data class Real(val names: List<CirName>) : ValueParameterNames()
class MultipleReal(valueParameters: List<CirValueParameter>) : ValueParameterNames() {
val generatedNames: List<Name> = generatedNames(valueParameters)
val generatedNames: List<CirName> = generatedNames(valueParameters)
}
companion object {
......@@ -87,7 +86,7 @@ class CallableValueParametersCommonizer(
var real = false
val names = callable.valueParameters.mapIndexed { index, valueParameter ->
val name = valueParameter.name
val plainName = name.asString()
val plainName = name.name
if (valueParameter.varargElementType != null) {
if (plainName != VARIADIC_ARGUMENTS) {
......@@ -107,7 +106,7 @@ class CallableValueParametersCommonizer(
return if (real) Real(names) else Generated
}
fun generatedNames(valueParameters: List<CirValueParameter>): List<Name> =
fun generatedNames(valueParameters: List<CirValueParameter>): List<CirName> =
valueParameters.mapIndexed { index, valueParameter ->
if (valueParameter.varargElementType != null) {
VARIADIC_ARGUMENTS_NAME
......@@ -223,9 +222,9 @@ class CallableValueParametersCommonizer(
private const val VARIADIC_ARGUMENTS = "variadicArguments"
private const val REGULAR_ARGUMENT_PREFIX = "arg"
private val VARIADIC_ARGUMENTS_NAME = Name.identifier(VARIADIC_ARGUMENTS).intern()
private val REGULAR_ARGUMENT_NAMES = FactoryMap.create<Int, Name> { index ->
Name.identifier(REGULAR_ARGUMENT_PREFIX + index).intern()
private val VARIADIC_ARGUMENTS_NAME = CirName.create(VARIADIC_ARGUMENTS)
private val REGULAR_ARGUMENT_NAMES = FactoryMap.create<Int, CirName> { index ->
CirName.create(REGULAR_ARGUMENT_PREFIX + index)
}
private fun CirCallableMemberWithParameters.canNamesBeOverwritten(): Boolean {
......
......@@ -7,12 +7,12 @@ package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirClassFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.name.Name
class ClassCommonizer(classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirClass, CirClass>() {
private lateinit var name: Name
private lateinit var name: CirName
private lateinit var kind: ClassKind
private val typeParameters = TypeParameterListCommonizer(classifiers)
private val modality = ModalityCommonizer()
......
......@@ -6,13 +6,12 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMapNotNull
import org.jetbrains.kotlin.descriptors.commonizer.utils.internedClassId
import org.jetbrains.kotlin.name.ClassId
internal class CommonizationVisitor(
private val classifiers: CirKnownClassifiers,
......@@ -88,7 +87,7 @@ internal class CommonizationVisitor(
// companion object should have the same name for each target class, then it could be set to common class
val companionObjectName = node.targetDeclarations.mapTo(HashSet()) { it!!.companion }.singleOrNull()
if (companionObjectName != null) {
val companionObjectClassId = internedClassId(node.classId, companionObjectName)
val companionObjectClassId = node.classifierId.createNestedEntityId(companionObjectName)
val companionObjectNode = classifiers.commonized.classNode(companionObjectClassId)
?: error("Can't find companion object with class ID $companionObjectClassId")
......@@ -99,7 +98,7 @@ internal class CommonizationVisitor(
}
// find out common (and commonized) supertypes
commonClass.commonizeSupertypes(node.classId, node.collectCommonSupertypes())
commonClass.commonizeSupertypes(node.classifierId, node.collectCommonSupertypes())
}
}
......@@ -112,7 +111,7 @@ internal class CommonizationVisitor(
if (commonClassifier is CirClass) {
// find out common (and commonized) supertypes
commonClassifier.commonizeSupertypes(node.classId, node.collectCommonSupertypes())
commonClassifier.commonizeSupertypes(node.classifierId, node.collectCommonSupertypes())
}
}
......@@ -135,7 +134,7 @@ internal class CommonizationVisitor(
val expandedClassNode = classifiers.commonized.classNode(expandedClassId) ?: return null
val expandedClass = expandedClassNode.targetDeclarations[index]
?: error("Can't find expanded class with class ID $expandedClassId and index $index for type alias $classId")
?: error("Can't find expanded class with class ID $expandedClassId and index $index for type alias $classifierId")
for (supertype in expandedClass.supertypes) {
supertypesMap.getOrPut(supertype) { CommonizedGroup(targetDeclarations.size) }[index] = supertype
......@@ -145,7 +144,7 @@ internal class CommonizationVisitor(
}
private fun CirClass.commonizeSupertypes(
classId: ClassId,
classId: CirEntityId,
supertypesMap: Map<CirType, CommonizedGroup<CirType>>?
) {
val commonSupertypes = supertypesMap?.values?.compactMapNotNull { supertypesGroup ->
......
......@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirModule
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirModuleFactory
import org.jetbrains.kotlin.name.Name
class ModuleCommonizer : AbstractStandardCommonizer<CirModule, CirModule>() {
private lateinit var name: Name
private lateinit var name: CirName
override fun commonizationResult() = CirModuleFactory.create(name = name)
......
......@@ -6,16 +6,16 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackage
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirPackageFactory
import org.jetbrains.kotlin.name.FqName
class PackageCommonizer : AbstractStandardCommonizer<CirPackage, CirPackage>() {
private lateinit var fqName: FqName
private lateinit var packageName: CirPackageName
override fun commonizationResult() = CirPackageFactory.create(fqName = fqName)
override fun commonizationResult() = CirPackageFactory.create(packageName = packageName)
override fun initialize(first: CirPackage) {
fqName = first.fqName
packageName = first.packageName
}
override fun doCommonizeWith(next: CirPackage) = true
......
......@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirClassFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeAliasFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.name.Name
/**
* Primary (optimistic) branch:
......@@ -45,7 +44,7 @@ class TypeAliasCommonizer(classifiers: CirKnownClassifiers) : AbstractStandardCo
private class TypeAliasShortCircuitingCommonizer(
private val classifiers: CirKnownClassifiers
) : AbstractStandardCommonizer<CirTypeAlias, CirTypeAlias>() {
private lateinit var name: Name
private lateinit var name: CirName
private val typeParameters = TypeParameterListCommonizer(classifiers)
private var underlyingType: CirClassOrTypeAliasType? = null // null means not computed yet
private val expandedType = TypeCommonizer(classifiers)
......@@ -79,7 +78,7 @@ private class TypeAliasShortCircuitingCommonizer(
}
private class TypeAliasLiftingUpCommonizer(classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirTypeAlias, CirTypeAlias>() {
private lateinit var name: Name
private lateinit var name: CirName
private val typeParameters = TypeParameterListCommonizer(classifiers)
private val underlyingType = TypeCommonizer(classifiers)
private val visibility = VisibilityCommonizer.lowering()
......@@ -111,7 +110,7 @@ private class TypeAliasLiftingUpCommonizer(classifiers: CirKnownClassifiers) : A
}
private class TypeAliasExpectClassCommonizer : AbstractStandardCommonizer<CirTypeAlias, CirClass>() {
private lateinit var name: Name
private lateinit var name: CirName
private val classVisibility = VisibilityCommonizer.equalizing()
override fun commonizationResult(): CirClass = CirClassFactory.create(
......
......@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.core.CommonizedTypeAliasAnswe
import org.jetbrains.kotlin.descriptors.commonizer.core.CommonizedTypeAliasAnswer.Companion.SUCCESS_FROM_DEPENDEE_LIBRARY
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.isUnderKotlinNativeSyntheticPackages
import org.jetbrains.kotlin.name.ClassId
class TypeCommonizer(private val classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirType, CirType>() {
private lateinit var wrapped: Commonizer<*, CirType>
......@@ -38,7 +37,7 @@ class TypeCommonizer(private val classifiers: CirKnownClassifiers) : AbstractSta
}
private class ClassTypeCommonizer(private val classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirClassType, CirClassType>() {
private lateinit var classId: ClassId
private lateinit var classId: CirEntityId
private val outerType = OuterClassTypeCommonizer(classifiers)
private lateinit var anyVisibility: DescriptorVisibility
private val arguments = TypeArgumentListCommonizer(classifiers)
......@@ -80,7 +79,7 @@ private class OuterClassTypeCommonizer(classifiers: CirKnownClassifiers) :
private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifiers) :
AbstractStandardCommonizer<CirTypeAliasType, CirClassOrTypeAliasType>() {
private lateinit var typeAliasId: ClassId
private lateinit var typeAliasId: CirEntityId
private val arguments = TypeArgumentListCommonizer(classifiers)
private var isMarkedNullable = false
private var commonizedTypeBuilder: CommonizedTypeAliasTypeBuilder? = null // null means not selected yet
......@@ -122,12 +121,12 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
// builds a new type for "common" library fragment for the given combination of type alias types in "platform" fragments
private interface CommonizedTypeAliasTypeBuilder {
fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirClassOrTypeAliasType
fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirClassOrTypeAliasType
companion object {
// type alias has been commonized to expect class, need to build type for expect class
fun forClass(commonClass: CirClass) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean) =
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean) =
CirTypeFactory.createClassType(
classId = typeAliasId,
outerType = null, // there can't be outer type
......@@ -143,7 +142,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
// type alias don't needs to be commonized because it is from the standard library
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: ClassId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
val underlyingTypeWithProperNullability = if (isMarkedNullable && !underlyingType.isMarkedNullable)
CirTypeFactory.makeNullable(underlyingType)
else
......@@ -192,11 +191,11 @@ private class FlexibleTypeCommonizer(classifiers: CirKnownClassifiers) : Abstrac
lowerBound.commonizeWith(next.lowerBound) && upperBound.commonizeWith(next.upperBound)
}
private fun commonizeClass(classId: ClassId, classifiers: CirKnownClassifiers): Boolean {
private fun commonizeClass(classId: CirEntityId, classifiers: CirKnownClassifiers): Boolean {
if (classifiers.commonDependeeLibraries.hasClassifier(classId)) {
// The class is from common fragment of dependee library (ex: stdlib). Already commonized.
return true
} else if (classId.packageFqName.isUnderKotlinNativeSyntheticPackages) {
} else if (classId.packageName.isUnderKotlinNativeSyntheticPackages) {
// C/Obj-C forward declarations are:
// - Either resolved to real classes/interfaces from other interop libraries (which are generated by C-interop tool and
// are known to have modality/visibility/other attributes to successfully pass commonization).
......@@ -219,7 +218,7 @@ private fun commonizeClass(classId: ClassId, classifiers: CirKnownClassifiers):
}
}
private fun commonizeTypeAlias(typeAliasId: ClassId, classifiers: CirKnownClassifiers): CommonizedTypeAliasAnswer {
private fun commonizeTypeAlias(typeAliasId: CirEntityId, classifiers: CirKnownClassifiers): CommonizedTypeAliasAnswer {
if (classifiers.commonDependeeLibraries.hasClassifier(typeAliasId)) {
// The type alias is from common fragment of dependee library (ex: stdlib). Already commonized.
return SUCCESS_FROM_DEPENDEE_LIBRARY
......
......@@ -5,15 +5,15 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeParameterFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
class TypeParameterCommonizer(classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirTypeParameter, CirTypeParameter>() {
private lateinit var name: Name
private lateinit var name: CirName
private var isReified = false
private lateinit var variance: Variance
private val upperBounds = TypeParameterUpperBoundsCommonizer(classifiers)
......
......@@ -5,15 +5,15 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirValueParameterFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.isNull
import org.jetbrains.kotlin.name.Name
class ValueParameterCommonizer(classifiers: CirKnownClassifiers) : AbstractStandardCommonizer<CirValueParameter, CirValueParameter>() {
private lateinit var name: Name
private lateinit var name: CirName
private val returnType = TypeCommonizer(classifiers)
private var varargElementType: CirType? = null
private var isCrossinline = true
......@@ -49,7 +49,7 @@ class ValueParameterCommonizer(classifiers: CirKnownClassifiers) : AbstractStand
return result
}
fun overwriteName(name: Name) {
fun overwriteName(name: CirName) {
this.name = name
}
}
......@@ -5,14 +5,14 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.name.Name
class ValueParameterListCommonizer(classifiers: CirKnownClassifiers) : AbstractListCommonizer<CirValueParameter, CirValueParameter>(
singleElementCommonizerFactory = { ValueParameterCommonizer(classifiers) }
) {
fun overwriteNames(names: List<Name>) {
fun overwriteNames(names: List<CirName>) {
forEachSingleElementCommonizer { index, singleElementCommonizer ->
(singleElementCommonizer as ValueParameterCommonizer).overwriteName(names[index])
}
......
......@@ -7,21 +7,21 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import gnu.trove.THashMap
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClass
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.NullableLazyValue
class CirClassNode(
override val targetDeclarations: CommonizedGroup<CirClass>,
override val commonDeclaration: NullableLazyValue<CirClass>,
override val classId: ClassId
) : CirNodeWithClassId<CirClass, CirClass>, CirNodeWithMembers<CirClass, CirClass> {
override val classifierId: CirEntityId
) : CirNodeWithClassifierId<CirClass, CirClass>, CirNodeWithMembers<CirClass, CirClass> {
val constructors: MutableMap<ConstructorApproximationKey, CirClassConstructorNode> = THashMap()
override val properties: MutableMap<PropertyApproximationKey, CirPropertyNode> = THashMap()
override val functions: MutableMap<FunctionApproximationKey, CirFunctionNode> = THashMap()
override val classes: MutableMap<Name, CirClassNode> = THashMap()
override val classes: MutableMap<CirName, CirClassNode> = THashMap()
override fun <T, R> accept(visitor: CirNodeVisitor<T, R>, data: T): R =
visitor.visitClassNode(this, data)
......
......@@ -7,15 +7,15 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import gnu.trove.THashMap
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirModule
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.storage.NullableLazyValue
class CirModuleNode(
override val targetDeclarations: CommonizedGroup<CirModule>,
override val commonDeclaration: NullableLazyValue<CirModule>
) : CirNode<CirModule, CirModule> {
val packages: MutableMap<FqName, CirPackageNode> = THashMap()
val packages: MutableMap<CirPackageName, CirPackageNode> = THashMap()
override fun <T, R> accept(visitor: CirNodeVisitor<T, R>, data: T) =
visitor.visitModuleNode(this, data)
......
......@@ -5,12 +5,8 @@
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassifier
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirDeclaration
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirLiftedUpDeclaration
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.NullableLazyValue
interface CirNode<T : CirDeclaration, R : CirDeclaration> {
......@@ -28,10 +24,10 @@ interface CirNode<T : CirDeclaration, R : CirDeclaration> {
fun toString(node: CirNode<*, *>) = buildString {
if (node is CirPackageNode) {
append("packageFqName=").append(node.packageFqName.asString()).append(", ")
append("packageName=").append(node.packageName).append(", ")
}
if (node is CirNodeWithClassId) {
append("classId=").append(node.classId.asString()).append(", ")
if (node is CirNodeWithClassifierId) {
append("classifierId=").append(node.classifierId).append(", ")
}
append("target=")
node.targetDeclarations.joinTo(this)
......@@ -41,8 +37,8 @@ interface CirNode<T : CirDeclaration, R : CirDeclaration> {
}
}
interface CirNodeWithClassId<T : CirClassifier, R : CirClassifier> : CirNode<T, R> {
val classId: ClassId
interface CirNodeWithClassifierId<T : CirClassifier, R : CirClassifier> : CirNode<T, R> {
val classifierId: CirEntityId
}
interface CirNodeWithLiftingUp<T : CirDeclaration, R : CirDeclaration> : CirNode<T, R> {
......@@ -53,5 +49,5 @@ interface CirNodeWithLiftingUp<T : CirDeclaration, R : CirDeclaration> : CirNode
interface CirNodeWithMembers<T : CirDeclaration, R : CirDeclaration> : CirNode<T, R> {
val properties: MutableMap<PropertyApproximationKey, CirPropertyNode>
val functions: MutableMap<FunctionApproximationKey, CirFunctionNode>
val classes: MutableMap<Name, CirClassNode>
val classes: MutableMap<CirName, CirClassNode>
}
......@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import gnu.trove.THashMap
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackage
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.descriptors.commonizer.utils.firstNonNull
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.NullableLazyValue
class CirPackageNode(
......@@ -20,11 +20,11 @@ class CirPackageNode(
override val properties: MutableMap<PropertyApproximationKey, CirPropertyNode> = THashMap()
override val functions: MutableMap<FunctionApproximationKey, CirFunctionNode> = THashMap()
override val classes: MutableMap<Name, CirClassNode> = THashMap()
val typeAliases: MutableMap<Name, CirTypeAliasNode> = THashMap()
override val classes: MutableMap<CirName, CirClassNode> = THashMap()
val typeAliases: MutableMap<CirName, CirTypeAliasNode> = THashMap()
val packageFqName: FqName
get() = targetDeclarations.firstNonNull().fqName
val packageName: CirPackageName
get() = targetDeclarations.firstNonNull().packageName
override fun <T, R> accept(visitor: CirNodeVisitor<T, R>, data: T) =
visitor.visitPackageNode(this, data)
......
......@@ -7,17 +7,17 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import gnu.trove.THashMap
import org.jetbrains.kotlin.descriptors.commonizer.CommonizerTarget
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirRoot
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirNode.Companion.indexOfCommon
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.storage.NullableLazyValue
class CirRootNode(
override val targetDeclarations: CommonizedGroup<CirRoot>,
override val commonDeclaration: NullableLazyValue<CirRoot>
) : CirNode<CirRoot, CirRoot> {
val modules: MutableMap<Name, CirModuleNode> = THashMap()
val modules: MutableMap<CirName, CirModuleNode> = THashMap()
fun getTarget(targetIndex: Int): CommonizerTarget =
(if (targetIndex == indexOfCommon) commonDeclaration() else targetDeclarations[targetIndex])!!.target
......
......@@ -10,12 +10,10 @@ import org.jetbrains.kotlin.descriptors.commonizer.LeafTarget
import org.jetbrains.kotlin.descriptors.commonizer.ModulesProvider.ModuleInfo
import org.jetbrains.kotlin.descriptors.commonizer.CommonizerParameters
import org.jetbrains.kotlin.descriptors.commonizer.TargetProvider
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.descriptors.commonizer.utils.internedClassId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
......@@ -123,27 +121,27 @@ class CirTreeMerger(
) {
processCInteropModuleAttributes(moduleInfo)
val moduleName: Name = moduleDescriptor.name.intern()
val moduleName: CirName = CirName.create(moduleDescriptor.name)
val moduleNode: CirModuleNode = rootNode.modules.getOrPut(moduleName) {
buildModuleNode(storageManager, size)
}
moduleNode.targetDeclarations[targetIndex] = CirModuleFactory.create(moduleName)
moduleDescriptor.collectNonEmptyPackageMemberScopes { packageFqName, packageMemberScope ->
processPackage(moduleNode, targetIndex, packageFqName.intern(), packageMemberScope)
moduleDescriptor.collectNonEmptyPackageMemberScopes { packageName, packageMemberScope ->
processPackage(moduleNode, targetIndex, packageName, packageMemberScope)
}
}
private fun processPackage(
moduleNode: CirModuleNode,
targetIndex: Int,
packageFqName: FqName,
packageName: CirPackageName,
packageMemberScope: MemberScope
) {
val packageNode: CirPackageNode = moduleNode.packages.getOrPut(packageFqName) {
val packageNode: CirPackageNode = moduleNode.packages.getOrPut(packageName) {
buildPackageNode(storageManager, size)
}
packageNode.targetDeclarations[targetIndex] = CirPackageFactory.create(packageFqName)
packageNode.targetDeclarations[targetIndex] = CirPackageFactory.create(packageName)
packageMemberScope.collectMembers(
PropertyCollector { propertyDescriptor ->
......@@ -154,7 +152,7 @@ class CirTreeMerger(
},
ClassCollector { classDescriptor ->
processClass(packageNode, targetIndex, classDescriptor) { className ->
internedClassId(packageFqName, className)
CirEntityId.create(packageName, className)
}
},
TypeAliasCollector { typeAliasDescriptor ->
......@@ -195,9 +193,9 @@ class CirTreeMerger(
ownerNode: CirNodeWithMembers<*, *>,
targetIndex: Int,
classDescriptor: ClassDescriptor,
classIdFunction: (Name) -> ClassId
classIdFunction: (CirName) -> CirEntityId
) {
val className = classDescriptor.name.intern()
val className = CirName.create(classDescriptor.name)
val classId = classIdFunction(className)
val classNode: CirClassNode = ownerNode.classes.getOrPut(className) {
......@@ -220,7 +218,7 @@ class CirTreeMerger(
},
ClassCollector { nestedClassDescriptor ->
processClass(classNode, targetIndex, nestedClassDescriptor) { nestedClassName ->
internedClassId(classId, nestedClassName)
classId.createNestedEntityId(nestedClassName)
}
}
)
......@@ -245,8 +243,8 @@ class CirTreeMerger(
targetIndex: Int,
typeAliasDescriptor: TypeAliasDescriptor
) {
val typeAliasName = typeAliasDescriptor.name.intern()
val typeAliasClassId = internedClassId(packageNode.packageFqName, typeAliasName)
val typeAliasName = CirName.create(typeAliasDescriptor.name)
val typeAliasClassId = CirEntityId.create(packageNode.packageName, typeAliasName)
val typeAliasNode: CirTypeAliasNode = packageNode.typeAliases.getOrPut(typeAliasName) {
buildTypeAliasNode(storageManager, size, classifiers, typeAliasClassId)
......@@ -257,12 +255,12 @@ class CirTreeMerger(
private fun processCInteropModuleAttributes(moduleInfo: ModuleInfo) {
val cInteropAttributes = moduleInfo.cInteropAttributes ?: return
val exportForwardDeclarations = cInteropAttributes.exportForwardDeclarations.takeIf { it.isNotEmpty() } ?: return
val mainPackageFqName = FqName(cInteropAttributes.mainPackageFqName).intern()
val mainPackageFqName = CirPackageName.create(cInteropAttributes.mainPackageFqName)
exportForwardDeclarations.forEach { classFqName ->
// Class has synthetic package FQ name (cnames/objcnames). Need to transfer it to the main package.
val className = Name.identifier(classFqName.substringAfterLast('.')).intern()
classifiers.forwardDeclarations.addExportedForwardDeclaration(internedClassId(mainPackageFqName, className))
val className = CirName.create(classFqName.substringAfterLast('.'))
classifiers.forwardDeclarations.addExportedForwardDeclaration(CirEntityId.create(mainPackageFqName, className))
}
}
}
......@@ -6,16 +6,16 @@
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassifier
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAlias
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.storage.NullableLazyValue
class CirTypeAliasNode(
override val targetDeclarations: CommonizedGroup<CirTypeAlias>,
override val commonDeclaration: NullableLazyValue<CirClassifier>,
override val classId: ClassId
) : CirNodeWithClassId<CirTypeAlias, CirClassifier>, CirNodeWithLiftingUp<CirTypeAlias, CirClassifier> {
override val classifierId: CirEntityId
) : CirNodeWithClassifierId<CirTypeAlias, CirClassifier>, CirNodeWithLiftingUp<CirTypeAlias, CirClassifier> {
override fun <T, R> accept(visitor: CirNodeVisitor<T, R>, data: T): R =
visitor.visitTypeAliasNode(this, data)
......
......@@ -6,33 +6,32 @@
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.*
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.utils.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.descriptors.commonizer.utils.signature
import org.jetbrains.kotlin.name.Name
/** Used for approximation of [PropertyDescriptor]s before running concrete [Commonizer]s */
data class PropertyApproximationKey(
val name: Name,
val name: CirName,
val extensionReceiverParameterType: CirTypeSignature?
) {
constructor(property: PropertyDescriptor) : this(
property.name.intern(),
CirName.create(property.name),
property.extensionReceiverParameter?.type?.signature
)
}
/** Used for approximation of [SimpleFunctionDescriptor]s before running concrete [Commonizer]s */
data class FunctionApproximationKey(
val name: Name,
val name: CirName,
val valueParametersTypes: Array<CirTypeSignature>,
private val additionalValueParametersNamesHash: Int,
val extensionReceiverParameterType: CirTypeSignature?
) {
constructor(function: SimpleFunctionDescriptor) : this(
function.name.intern(),
CirName.create(function.name),
function.valueParameters.toTypeSignatures(),
additionalValueParameterNamesHash(function),
function.extensionReceiverParameter?.type?.signature
......
......@@ -10,11 +10,10 @@ import gnu.trove.THashSet
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.SharedTarget
import org.jetbrains.kotlin.descriptors.commonizer.CommonizerTarget
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.utils.isUnderKotlinNativeSyntheticPackages
import org.jetbrains.kotlin.descriptors.commonizer.utils.resolveClassOrTypeAlias
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
......@@ -31,27 +30,27 @@ class CirKnownClassifiers(
interface CirCommonizedClassifiers {
/* Accessors */
fun classNode(classId: ClassId): CirClassNode?
fun typeAliasNode(typeAliasId: ClassId): CirTypeAliasNode?
fun classNode(classId: CirEntityId): CirClassNode?
fun typeAliasNode(typeAliasId: CirEntityId): CirTypeAliasNode?
/* Mutators */
fun addClassNode(classId: ClassId, node: CirClassNode)
fun addTypeAliasNode(typeAliasId: ClassId, node: CirTypeAliasNode)
fun addClassNode(classId: CirEntityId, node: CirClassNode)
fun addTypeAliasNode(typeAliasId: CirEntityId, node: CirTypeAliasNode)
companion object {
fun default() = object : CirCommonizedClassifiers {
private val classNodes = THashMap<ClassId, CirClassNode>()
private val typeAliases = THashMap<ClassId, CirTypeAliasNode>()
private val classNodes = THashMap<CirEntityId, CirClassNode>()
private val typeAliases = THashMap<CirEntityId, CirTypeAliasNode>()
override fun classNode(classId: ClassId) = classNodes[classId]
override fun typeAliasNode(typeAliasId: ClassId) = typeAliases[typeAliasId]
override fun classNode(classId: CirEntityId) = classNodes[classId]
override fun typeAliasNode(typeAliasId: CirEntityId) = typeAliases[typeAliasId]
override fun addClassNode(classId: ClassId, node: CirClassNode) {
override fun addClassNode(classId: CirEntityId, node: CirClassNode) {
val oldNode = classNodes.put(classId, node)
check(oldNode == null) { "Rewriting class node $classId" }
}
override fun addTypeAliasNode(typeAliasId: ClassId, node: CirTypeAliasNode) {
override fun addTypeAliasNode(typeAliasId: CirEntityId, node: CirTypeAliasNode) {
val oldNode = typeAliases.put(typeAliasId, node)
check(oldNode == null) { "Rewriting type alias node $typeAliasId" }
}
......@@ -61,19 +60,19 @@ interface CirCommonizedClassifiers {
interface CirForwardDeclarations {
/* Accessors */
fun isExportedForwardDeclaration(classId: ClassId): Boolean
fun isExportedForwardDeclaration(classId: CirEntityId): Boolean
/* Mutators */
fun addExportedForwardDeclaration(classId: ClassId)
fun addExportedForwardDeclaration(classId: CirEntityId)
companion object {
fun default() = object : CirForwardDeclarations {
private val exportedForwardDeclarations = THashSet<ClassId>()
private val exportedForwardDeclarations = THashSet<CirEntityId>()
override fun isExportedForwardDeclaration(classId: ClassId) = classId in exportedForwardDeclarations
override fun isExportedForwardDeclaration(classId: CirEntityId) = classId in exportedForwardDeclarations
override fun addExportedForwardDeclaration(classId: ClassId) {
check(!classId.packageFqName.isUnderKotlinNativeSyntheticPackages)
override fun addExportedForwardDeclaration(classId: CirEntityId) {
check(!classId.packageName.isUnderKotlinNativeSyntheticPackages)
exportedForwardDeclarations += classId
}
}
......@@ -81,45 +80,43 @@ interface CirForwardDeclarations {
}
interface CirProvidedClassifiers {
fun hasClassifier(classifierId: ClassId): Boolean
fun hasClassifier(classifierId: CirEntityId): Boolean
// TODO: implement later
//fun classifier(classifierId: ClassId): Any?
companion object {
internal val EMPTY = object : CirProvidedClassifiers {
override fun hasClassifier(classifierId: ClassId) = false
override fun hasClassifier(classifierId: CirEntityId) = false
}
// N.B. This is suboptimal implementation. It will be replaced by another implementation that will
// retrieve classifier information directly from the metadata.
fun fromModules(storageManager: StorageManager, modules: () -> Collection<ModuleDescriptor>) = object : CirProvidedClassifiers {
private val nonEmptyMemberScopes: Map<FqName, MemberScope> by storageManager.createLazyValue {
THashMap<FqName, MemberScope>().apply {
private val nonEmptyMemberScopes: Map<CirPackageName, MemberScope> by storageManager.createLazyValue {
THashMap<CirPackageName, MemberScope>().apply {
for (module in modules()) {
module.collectNonEmptyPackageMemberScopes(probeRootPackageForEmptiness = true) { packageFqName, memberScope ->
this[packageFqName.intern()] = memberScope
module.collectNonEmptyPackageMemberScopes(probeRootPackageForEmptiness = true) { packageName, memberScope ->
this[packageName] = memberScope
}
}
}
}
private val presentClassifiers = THashSet<ClassId>()
private val missingClassifiers = THashSet<ClassId>()
private val presentClassifiers = THashSet<CirEntityId>()
private val missingClassifiers = THashSet<CirEntityId>()
override fun hasClassifier(classifierId: ClassId): Boolean {
val relativeClassName: FqName = classifierId.relativeClassName
if (relativeClassName.isRoot)
override fun hasClassifier(classifierId: CirEntityId): Boolean {
if (classifierId.relativeNameSegments.isEmpty())
return false
val packageFqName = classifierId.packageFqName
val memberScope = nonEmptyMemberScopes[packageFqName] ?: return false
val memberScope = nonEmptyMemberScopes[classifierId.packageName] ?: return false
return when (classifierId) {
in presentClassifiers -> true
in missingClassifiers -> false
else -> {
val found = memberScope.resolveClassOrTypeAlias(relativeClassName) != null
val found = memberScope.resolveClassOrTypeAlias(classifierId.relativeNameSegments) != null
when (found) {
true -> presentClassifiers += classifierId
false -> missingClassifiers += classifierId
......
......@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.backend.common.serialization.metadata.impl.ClassifierAliasingPackageFragmentDescriptor
import org.jetbrains.kotlin.backend.common.serialization.metadata.impl.ExportedForwardDeclarationsPackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.utils.*
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope
......@@ -65,7 +66,7 @@ internal inline fun FunctionCollector(
// collects member scopes for every non-empty package provided by this module
internal fun ModuleDescriptor.collectNonEmptyPackageMemberScopes(
probeRootPackageForEmptiness: Boolean = false, // false is the default as probing might be expensive and is not always necessary
collector: (FqName, MemberScope) -> Unit
collector: (CirPackageName, MemberScope) -> Unit
) {
// we don's need to process fragments from other modules which are the dependencies of this module, so
// let's use the appropriate package fragment provider
......@@ -88,7 +89,7 @@ internal fun ModuleDescriptor.collectNonEmptyPackageMemberScopes(
"package member scope for $packageFqName in $name",
ownPackageMemberScopes
)
collector(packageFqName, memberScope)
collector(CirPackageName.create(packageFqName), memberScope)
}
packageFragmentProvider.getSubPackagesOf(packageFqName, alwaysTrue()).toSet().map { recurse(it) }
......
......@@ -6,11 +6,11 @@
package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirDeclaration
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirClassRecursionMarker
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirClassifierRecursionMarker
import org.jetbrains.kotlin.descriptors.commonizer.core.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.storage.NullableLazyValue
import org.jetbrains.kotlin.storage.StorageManager
......@@ -75,7 +75,7 @@ internal fun buildClassNode(
size: Int,
classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?,
classId: ClassId
classId: CirEntityId
): CirClassNode = buildNode(
storageManager = storageManager,
size = size,
......@@ -106,7 +106,7 @@ internal fun buildTypeAliasNode(
storageManager: StorageManager,
size: Int,
classifiers: CirKnownClassifiers,
typeAliasId: ClassId
typeAliasId: CirEntityId
): CirTypeAliasNode = buildNode(
storageManager = storageManager,
size = size,
......
......@@ -16,15 +16,12 @@ import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeAliasExpansion.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.DEFAULT_SETTER_VALUE_NAME
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.strip
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
internal fun CirModule.buildModule(
fragments: Collection<KmModuleFragment>
): KlibModuleMetadata = KlibModuleMetadata(
name = name.strip(),
name = name.toStrippedString(),
fragments = fragments.toList(),
annotations = emptyList()
)
......@@ -35,7 +32,7 @@ internal fun CirPackage.buildModuleFragment(
topLevelFunctions: Collection<KmFunction>,
topLevelProperties: Collection<KmProperty>
): KmModuleFragment = KmModuleFragment().also { fragment ->
fragment.fqName = fqName.asString()
fragment.fqName = packageName.toString()
allClasses.forEach {
fragment.classes += it
fragment.className += it.name
......@@ -43,7 +40,7 @@ internal fun CirPackage.buildModuleFragment(
if (topLevelTypeAliases.isNotEmpty() || topLevelFunctions.isNotEmpty() || topLevelProperties.isNotEmpty()) {
fragment.pkg = KmPackage().also { pkg ->
pkg.fqName = fqName.asString()
pkg.fqName = packageName.toString()
pkg.typeAliases += topLevelTypeAliases
pkg.functions += topLevelFunctions
pkg.properties += topLevelProperties
......@@ -101,15 +98,15 @@ internal fun CirClass.buildClass(
}
}
clazz.companionObject = companion?.asString()
clazz.companionObject = companion?.name
supertypes.mapTo(clazz.supertypes) { it.buildType(context) }
}
internal fun linkSealedClassesWithSubclasses(packageFqName: FqName, classConsumer: ClassConsumer) {
internal fun linkSealedClassesWithSubclasses(packageName: CirPackageName, classConsumer: ClassConsumer) {
if (classConsumer.allClasses.isEmpty() || classConsumer.sealedClasses.isEmpty()) return
val packageName = packageFqName.asString().replace('.', '/')
fun ClassName.isInSamePackage(): Boolean = substringBeforeLast('/', "") == packageName
val metadataPackageName = packageName.toMetadataString()
fun ClassName.isInSamePackage(): Boolean = substringBeforeLast('/', "") == metadataPackageName
val sealedClassesMap: Map<ClassName, KmClass> = classConsumer.sealedClasses.associateBy { it.name }
......@@ -136,7 +133,7 @@ internal fun CirTypeAlias.buildTypeAlias(
context: MetadataBuildingVisitorContext
): KmTypeAlias = KmTypeAlias(
flags = typeAliasFlags(),
name = name.asString()
name = name.name
).also { typeAlias ->
annotations.mapTo(typeAlias.annotations) { it.buildAnnotation() }
typeParameters.buildTypeParameters(context, output = typeAlias.typeParameters)
......@@ -148,7 +145,7 @@ internal fun CirProperty.buildProperty(
context: MetadataBuildingVisitorContext,
): KmProperty = KmProperty(
flags = propertyFlags(isExpect = context.isCommon && !isLiftedUp),
name = name.asString(),
name = name.name,
getterFlags = getter?.propertyAccessorFlags(this, this) ?: NO_FLAGS,
setterFlags = setter?.let { setter -> setter.propertyAccessorFlags(setter, this) } ?: NO_FLAGS
).also { property ->
......@@ -180,7 +177,7 @@ internal fun CirFunction.buildFunction(
context: MetadataBuildingVisitorContext,
): KmFunction = KmFunction(
flags = functionFlags(isExpect = context.isCommon && kind != CallableMemberDescriptor.Kind.SYNTHESIZED),
name = name.asString()
name = name.name
).also { function ->
annotations.mapTo(function.annotations) { it.buildAnnotation() }
typeParameters.buildTypeParameters(context, output = function.typeParameters)
......@@ -195,17 +192,17 @@ internal fun CirFunction.buildFunction(
private fun CirAnnotation.buildAnnotation(): KmAnnotation {
val arguments = LinkedHashMap<String, KmAnnotationArgument<*>>(constantValueArguments.size + annotationValueArguments.size, 1F)
constantValueArguments.forEach { (name: Name, value: CirConstantValue<*>) ->
arguments[name.asString()] = value.buildAnnotationArgument()
constantValueArguments.forEach { (name: CirName, value: CirConstantValue<*>) ->
arguments[name.name] = value.buildAnnotationArgument()
?: error("Unexpected <null> constant value inside of $this")
}
annotationValueArguments.forEach { (name: Name, nested: CirAnnotation) ->
arguments[name.asString()] = KmAnnotationArgument.AnnotationValue(nested.buildAnnotation())
annotationValueArguments.forEach { (name: CirName, nested: CirAnnotation) ->
arguments[name.name] = KmAnnotationArgument.AnnotationValue(nested.buildAnnotation())
}
return KmAnnotation(
className = type.classifierId.asString(),
className = type.classifierId.toString(),
arguments = arguments
)
}
......@@ -228,7 +225,7 @@ private fun CirConstantValue<*>.buildAnnotationArgument(): KmAnnotationArgument<
is CirConstantValue.DoubleValue -> KmAnnotationArgument.DoubleValue(value)
is CirConstantValue.BooleanValue -> KmAnnotationArgument.BooleanValue(value)
is CirConstantValue.EnumValue -> KmAnnotationArgument.EnumValue(enumClassId.asString(), enumEntryName.asString())
is CirConstantValue.EnumValue -> KmAnnotationArgument.EnumValue(enumClassId.toString(), enumEntryName.name)
is CirConstantValue.NullValue -> null
is CirConstantValue.ArrayValue -> KmAnnotationArgument.ArrayValue(value.compactMap { element ->
......@@ -240,7 +237,7 @@ private fun CirValueParameter.buildValueParameter(
context: MetadataBuildingVisitorContext
): KmValueParameter = KmValueParameter(
flags = valueParameterFlags(),
name = name.asString()
name = name.name
).also { parameter ->
annotations.mapTo(parameter.annotations) { it.buildAnnotation() }
parameter.type = returnType.buildType(context)
......@@ -256,7 +253,7 @@ private fun List<CirTypeParameter>.buildTypeParameters(
mapIndexedTo(output) { index, cirTypeParameter ->
KmTypeParameter(
flags = cirTypeParameter.typeParameterFlags(),
name = cirTypeParameter.name.asString(),
name = cirTypeParameter.name.name,
id = context.typeParameterIndexOffset + index,
variance = cirTypeParameter.variance.buildVariance()
).also { parameter ->
......@@ -292,7 +289,7 @@ private fun CirClassType.buildType(
context: MetadataBuildingVisitorContext,
expansion: TypeAliasExpansion
): KmType = KmType(typeFlags()).also { type ->
type.classifier = KmClassifier.Class(classifierId.asString())
type.classifier = KmClassifier.Class(classifierId.toString())
arguments.mapTo(type.arguments) { it.buildArgument(context, expansion) }
outerType?.let { type.outerType = it.buildType(context, expansion) }
}
......@@ -316,7 +313,7 @@ private fun CirTypeAliasType.buildAbbreviationType(
expansion: TypeAliasExpansion
): KmType {
val abbreviationType = KmType(typeFlags())
abbreviationType.classifier = KmClassifier.TypeAlias(classifierId.asString())
abbreviationType.classifier = KmClassifier.TypeAlias(classifierId.toString())
arguments.mapTo(abbreviationType.arguments) { it.buildArgument(context, expansion) }
return abbreviationType
}
......
......@@ -15,10 +15,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.stats.DeclarationType
import org.jetbrains.kotlin.descriptors.commonizer.stats.StatsCollector
import org.jetbrains.kotlin.descriptors.commonizer.stats.StatsCollector.StatsKey
import org.jetbrains.kotlin.descriptors.commonizer.utils.DEFAULT_CONSTRUCTOR_NAME
import org.jetbrains.kotlin.descriptors.commonizer.utils.strip
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.descriptors.commonizer.utils.firstNonNull
import org.jetbrains.kotlin.descriptors.commonizer.metadata.MetadataBuildingVisitorContext.Path
......@@ -65,8 +61,8 @@ private class MetadataBuildingVisitor(
val cirModule = moduleContext.get<CirModule>(node) ?: return null
val fragments: MutableCollection<KmModuleFragment> = mutableListOf()
node.packages.mapNotNullTo(fragments) { (packageFqName, packageNode) ->
val packageContext = moduleContext.packageContext(packageFqName)
node.packages.mapNotNullTo(fragments) { (packageName, packageNode) ->
val packageContext = moduleContext.packageContext(packageName)
packageNode.accept(this, packageContext)?.cast()
}
......@@ -106,7 +102,7 @@ private class MetadataBuildingVisitor(
}
}
linkSealedClassesWithSubclasses(cirPackage.fqName, classConsumer)
linkSealedClassesWithSubclasses(cirPackage.packageName, classConsumer)
val topLevelFunctions: Collection<KmFunction> = node.functions.mapNotNull { (functionKey, functionNode) ->
val functionContext = packageContext.callableMemberContext(functionKey.name)
......@@ -221,12 +217,12 @@ private class MetadataBuildingVisitor(
Flag.Class.IS_ENUM_CLASS(clazz.flags) -> DeclarationType.ENUM_CLASS
Flag.Class.IS_ENUM_ENTRY(clazz.flags) -> DeclarationType.ENUM_ENTRY
Flag.Class.IS_INTERFACE(clazz.flags) -> when {
(classContext.currentPath as Path.Classifier).classifierId.isNestedClass -> DeclarationType.NESTED_INTERFACE
(classContext.currentPath as Path.Classifier).classifierId.isNestedEntity -> DeclarationType.NESTED_INTERFACE
else -> DeclarationType.TOP_LEVEL_INTERFACE
}
else -> when {
Flag.Class.IS_COMPANION_OBJECT(clazz.flags) -> DeclarationType.COMPANION_OBJECT
(classContext.currentPath as Path.Classifier).classifierId.isNestedClass -> DeclarationType.NESTED_CLASS
(classContext.currentPath as Path.Classifier).classifierId.isNestedEntity -> DeclarationType.NESTED_CLASS
else -> DeclarationType.TOP_LEVEL_CLASS
}
}
......@@ -246,7 +242,7 @@ private class MetadataBuildingVisitor(
propertyNode: CirPropertyNode
) = logDeclaration(propertyContext.targetIndex) {
val declarationType = when {
(propertyContext.currentPath as Path.CallableMember).memberId.isNestedClass -> DeclarationType.NESTED_VAL
(propertyContext.currentPath as Path.CallableMember).memberId.isNestedEntity -> DeclarationType.NESTED_VAL
propertyNode.targetDeclarations.firstNonNull().isConst -> DeclarationType.TOP_LEVEL_CONST_VAL
else -> DeclarationType.TOP_LEVEL_VAL
}
......@@ -266,7 +262,7 @@ private class MetadataBuildingVisitor(
functionKey: FunctionApproximationKey
) = logDeclaration(functionContext.targetIndex) {
val declarationType = when {
(functionContext.currentPath as Path.CallableMember).memberId.isNestedClass -> DeclarationType.NESTED_FUN
(functionContext.currentPath as Path.CallableMember).memberId.isNestedEntity -> DeclarationType.NESTED_FUN
else -> DeclarationType.TOP_LEVEL_FUN
}
......@@ -308,31 +304,30 @@ internal data class MetadataBuildingVisitorContext(
}
@Suppress("MemberVisibilityCanBePrivate")
class Module(val moduleName: Name) : Path() {
override fun toString() = moduleName.strip()
class Module(val moduleName: CirName) : Path() {
override fun toString() = moduleName.toStrippedString()
}
class Package(val packageFqName: FqName) : Path() {
fun nestedClassifier(classifierName: Name) = Classifier(ClassId(packageFqName, classifierName))
fun nestedCallableMember(memberName: Name) = CallableMember(ClassId(packageFqName, memberName))
class Package(val packageName: CirPackageName) : Path() {
fun nestedClassifier(classifierName: CirName) = Classifier(CirEntityId.create(packageName, classifierName))
fun nestedCallableMember(memberName: CirName) = CallableMember(CirEntityId.create(packageName, memberName))
override fun toString() = packageFqName.asString()
override fun toString() = packageName.toString()
}
class Classifier(val classifierId: ClassId) : Path() {
fun nestedClassifier(classifierName: Name) = Classifier(classifierId.createNestedClassId(classifierName))
fun nestedCallableMember(memberName: Name) = CallableMember(classifierId.createNestedClassId(memberName))
class Classifier(val classifierId: CirEntityId) : Path() {
fun nestedClassifier(classifierName: CirName) = Classifier(classifierId.createNestedEntityId(classifierName))
fun nestedCallableMember(memberName: CirName) = CallableMember(classifierId.createNestedEntityId(memberName))
override fun toString() = classifierId.asString()
override fun toString() = classifierId.toString()
}
class CallableMember(val memberId: ClassId) : Path() {
override fun toString() = memberId.asString()
class CallableMember(val memberId: CirEntityId) : Path() {
override fun toString() = memberId.toString()
}
}
fun moduleContext(moduleName: Name): MetadataBuildingVisitorContext {
check(moduleName.isSpecial)
fun moduleContext(moduleName: CirName): MetadataBuildingVisitorContext {
check(currentPath is Path.Empty)
return MetadataBuildingVisitorContext(
......@@ -344,7 +339,7 @@ internal data class MetadataBuildingVisitorContext(
)
}
fun packageContext(packageFqName: FqName): MetadataBuildingVisitorContext {
fun packageContext(packageName: CirPackageName): MetadataBuildingVisitorContext {
check(currentPath is Path.Module)
return MetadataBuildingVisitorContext(
......@@ -352,12 +347,12 @@ internal data class MetadataBuildingVisitorContext(
target = target,
isCommon = isCommon,
typeParameterIndexOffset = 0,
currentPath = Path.Package(packageFqName)
currentPath = Path.Package(packageName)
)
}
fun classifierContext(
classifierName: Name,
classifierName: CirName,
outerClassTypeParametersCount: Int = 0
): MetadataBuildingVisitorContext {
val newPath = when (currentPath) {
......@@ -382,7 +377,7 @@ internal data class MetadataBuildingVisitorContext(
}
fun callableMemberContext(
memberName: Name,
memberName: CirName,
ownerClassTypeParametersCount: Int = 0
): MetadataBuildingVisitorContext {
val newPath = when (currentPath) {
......
......@@ -8,72 +8,61 @@ package org.jetbrains.kotlin.descriptors.commonizer.utils
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.konan.impl.ForwardDeclarationsFqNames
internal val DEPRECATED_ANNOTATION_FQN: FqName = FqName(Deprecated::class.java.name).intern()
internal val DEPRECATED_ANNOTATION_CLASS_ID: ClassId = internedClassId(DEPRECATED_ANNOTATION_FQN)
internal val DEPRECATED_ANNOTATION_FQN: FqName = FqName(Deprecated::class.java.name)
internal val DEPRECATED_ANNOTATION_CLASS_ID: CirEntityId = CirEntityId.create("kotlin/Deprecated")
internal val ANY_CLASS_ID: ClassId = internedClassId(StandardNames.FqNames.any.toSafe().intern())
private val NOTHING_CLASS_ID: ClassId = internedClassId(StandardNames.FqNames.nothing.toSafe().intern())
internal val ANY_CLASS_ID: CirEntityId = CirEntityId.create("kotlin/Any")
private val NOTHING_CLASS_ID: CirEntityId = CirEntityId.create("kotlin/Nothing")
internal val SPECIAL_CLASS_WITHOUT_SUPERTYPES_CLASS_IDS = listOf(
internal val SPECIAL_CLASS_WITHOUT_SUPERTYPES_CLASS_IDS: List<CirEntityId> = listOf(
ANY_CLASS_ID,
NOTHING_CLASS_ID
)
private val STANDARD_KOTLIN_PACKAGES = listOf(
StandardNames.BUILT_INS_PACKAGE_FQ_NAME.asString(),
"kotlinx"
private val STANDARD_KOTLIN_PACKAGES: List<CirPackageName> = listOf(
CirPackageName.create(StandardNames.BUILT_INS_PACKAGE_FQ_NAME),
CirPackageName.create("kotlinx")
)
private val KOTLIN_NATIVE_SYNTHETIC_PACKAGES = ForwardDeclarationsFqNames.syntheticPackages
.map { fqName ->
check(!fqName.isRoot)
fqName.asString()
private val KOTLIN_NATIVE_SYNTHETIC_PACKAGES: List<CirPackageName> = ForwardDeclarationsFqNames.syntheticPackages
.map { packageFqName ->
check(!packageFqName.isRoot)
CirPackageName.create(packageFqName)
}
private const val CINTEROP_PACKAGE = "kotlinx.cinterop"
private val CINTEROP_PACKAGE: CirPackageName = CirPackageName.create("kotlinx.cinterop")
private val OBJC_INTEROP_CALLABLE_ANNOTATIONS = listOf(
"ObjCMethod",
"ObjCConstructor",
"ObjCFactory"
private val OBJC_INTEROP_CALLABLE_ANNOTATIONS: List<CirName> = listOf(
CirName.create("ObjCMethod"),
CirName.create("ObjCConstructor"),
CirName.create("ObjCFactory")
)
internal val DEFAULT_CONSTRUCTOR_NAME = Name.identifier("<init>").intern()
internal val DEFAULT_SETTER_VALUE_NAME = Name.identifier("value").intern()
internal val DEFAULT_CONSTRUCTOR_NAME: CirName = CirName.create("<init>")
internal val DEFAULT_SETTER_VALUE_NAME: CirName = CirName.create("value")
internal fun Name.strip(): String =
asString().removeSurrounding("<", ">")
internal val FqName.isUnderStandardKotlinPackages: Boolean
get() = hasAnyPrefix(STANDARD_KOTLIN_PACKAGES)
internal val CirPackageName.isUnderStandardKotlinPackages: Boolean
get() = STANDARD_KOTLIN_PACKAGES.any(::startsWith)
internal val FqName.isUnderKotlinNativeSyntheticPackages: Boolean
get() = hasAnyPrefix(KOTLIN_NATIVE_SYNTHETIC_PACKAGES)
internal val CirPackageName.isUnderKotlinNativeSyntheticPackages: Boolean
get() = KOTLIN_NATIVE_SYNTHETIC_PACKAGES.any(::startsWith)
@Suppress("NOTHING_TO_INLINE")
private inline fun FqName.hasAnyPrefix(prefixes: List<String>): Boolean =
asString().let { fqName -> prefixes.any(fqName::hasPrefix) }
private fun String.hasPrefix(prefix: String): Boolean {
val lengthDifference = length - prefix.length
return when {
lengthDifference == 0 -> this == prefix
lengthDifference > 0 -> this[prefix.length] == '.' && this.startsWith(prefix)
else -> false
}
}
internal val ClassId.isObjCInteropCallableAnnotation: Boolean
get() = packageFqName.asString() == CINTEROP_PACKAGE && relativeClassName.asString() in OBJC_INTEROP_CALLABLE_ANNOTATIONS
internal val CirEntityId.isObjCInteropCallableAnnotation: Boolean
get() = packageName == CINTEROP_PACKAGE && relativeNameSegments.singleOrNull() in OBJC_INTEROP_CALLABLE_ANNOTATIONS
internal val AnnotationDescriptor.isObjCInteropCallableAnnotation: Boolean
get() {
val classifier = type.declarationDescriptor
return classifier.name.asString() in OBJC_INTEROP_CALLABLE_ANNOTATIONS
&& (classifier.containingDeclaration as? PackageFragmentDescriptor)?.fqName?.asString() == CINTEROP_PACKAGE
return CirName.create(classifier.name) in OBJC_INTEROP_CALLABLE_ANNOTATIONS
&& (classifier.containingDeclaration as? PackageFragmentDescriptor)?.fqName?.let(CirPackageName::create) == CINTEROP_PACKAGE
}
/*
* 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.utils
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
internal fun FqName.intern(): FqName = fqNameInterner.intern(this)
internal fun Name.intern(): Name = nameInterner.intern(this)
internal fun ClassId.intern(): ClassId = classIdInterner.intern(this)
@Suppress("NOTHING_TO_INLINE")
internal inline fun internedClassId(topLevelFqName: FqName): ClassId {
val packageFqName = topLevelFqName.parent().intern()
val className = topLevelFqName.shortName().intern()
return internedClassId(packageFqName, className)
}
internal fun internedClassId(packageFqName: FqName, classifierName: Name): ClassId {
val relativeClassName = FqName.topLevel(classifierName).intern()
return ClassId(packageFqName, relativeClassName, false).intern()
}
internal fun internedClassId(ownerClassId: ClassId, nestedClassName: Name): ClassId {
val relativeClassName = ownerClassId.relativeClassName.child(nestedClassName).intern()
return ClassId(ownerClassId.packageFqName, relativeClassName, ownerClassId.isLocal).intern()
}
private val fqNameInterner = Interner<FqName>()
private val nameInterner = Interner<Name>()
private val classIdInterner = Interner<ClassId>()
......@@ -8,12 +8,13 @@ package org.jetbrains.kotlin.descriptors.commonizer.utils
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.konan.KonanBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.konan.util.KlibMetadataFactories
import org.jetbrains.kotlin.library.metadata.NativeTypeTransformer
import org.jetbrains.kotlin.library.metadata.NullFlexibleTypeDeserializer
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.serialization.konan.impl.KlibResolvedModuleDescriptorsFactoryImpl
import org.jetbrains.kotlin.storage.StorageManager
......@@ -31,27 +32,29 @@ internal fun createKotlinNativeForwardDeclarationsModule(
storageManager = storageManager
)
internal fun MemberScope.resolveClassOrTypeAlias(relativeClassName: FqName): ClassifierDescriptorWithTypeParameters? {
internal fun MemberScope.resolveClassOrTypeAlias(relativeNameSegments: Array<CirName>): ClassifierDescriptorWithTypeParameters? {
var memberScope: MemberScope = this
if (memberScope is MemberScope.Empty)
return null
val classifierName = if ('.' in relativeClassName.asString()) {
// resolve member scope of the nested class
relativeClassName.pathSegments().reduce { first, second ->
memberScope = (memberScope.getContributedClassifier(
first,
NoLookupLocation.FOR_ALREADY_TRACKED
) as? ClassDescriptor)?.unsubstitutedMemberScope ?: return null
second
val classifierName = when (relativeNameSegments.size) {
0 -> return null
1 -> relativeNameSegments[0]
else -> {
// resolve member scope of the nested class
relativeNameSegments.reduce { first, second ->
memberScope = (memberScope.getContributedClassifier(
Name.identifier(first.name),
NoLookupLocation.FOR_ALREADY_TRACKED
) as? ClassDescriptor)?.unsubstitutedMemberScope ?: return null
second
}
}
} else {
relativeClassName.shortName()
}
return memberScope.getContributedClassifier(
classifierName,
Name.identifier(classifierName.name),
NoLookupLocation.FOR_ALREADY_TRACKED
) as? ClassifierDescriptorWithTypeParameters
}
......
......@@ -9,8 +9,10 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
......@@ -30,10 +32,13 @@ internal fun extractExpandedType(abbreviated: AbbreviatedType): SimpleType {
return expanded
}
internal val ClassifierDescriptorWithTypeParameters.internedClassId: ClassId
internal val ClassifierDescriptorWithTypeParameters.classifierId: CirEntityId
get() = when (val owner = containingDeclaration) {
is PackageFragmentDescriptor -> internedClassId(owner.fqName.intern(), name.intern())
is ClassDescriptor -> internedClassId(owner.internedClassId, name.intern())
is PackageFragmentDescriptor -> CirEntityId.create(
packageName = CirPackageName.create(owner.fqName),
relativeName = CirName.create(name)
)
is ClassDescriptor -> owner.classifierId.createNestedEntityId(CirName.create(name))
else -> error("Unexpected containing declaration type for $this: ${owner::class}, $owner")
}
......
......@@ -5,16 +5,11 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirConstantValue.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirAnnotationFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.utils.mockClassType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.junit.Test
import kotlin.DeprecationLevel.*
......@@ -285,8 +280,8 @@ class AnnotationsCommonizerTest : AbstractCommonizerTest<List<CirAnnotation>, Li
private fun mockAnnotation(
fqName: String,
constantValueArguments: Map<Name, CirConstantValue<*>> = emptyMap(),
annotationValueArguments: Map<Name, CirAnnotation> = emptyMap()
constantValueArguments: Map<CirName, CirConstantValue<*>> = emptyMap(),
annotationValueArguments: Map<CirName, CirAnnotation> = emptyMap()
): CirAnnotation = CirAnnotationFactory.create(
type = CirTypeFactory.create(mockClassType(fqName)) as CirClassType,
constantValueArguments = constantValueArguments,
......@@ -303,8 +298,8 @@ private fun mockDeprecated(
mockAnnotation(
fqName = "kotlin.ReplaceWith",
constantValueArguments = mapOf(
Name.identifier("expression") to StringValue(replaceWithExpression),
Name.identifier("imports") to ArrayValue(replaceWithImports.map(::StringValue))
CirName.create("expression") to StringValue(replaceWithExpression),
CirName.create("imports") to ArrayValue(replaceWithImports.map(::StringValue))
),
annotationValueArguments = emptyMap()
)
......@@ -312,15 +307,12 @@ private fun mockDeprecated(
return mockAnnotation(
fqName = "kotlin.Deprecated",
constantValueArguments = HashMap<Name, CirConstantValue<*>>().apply {
this[Name.identifier("message")] = StringValue(message)
constantValueArguments = HashMap<CirName, CirConstantValue<*>>().apply {
this[CirName.create("message")] = StringValue(message)
if (level != WARNING)
this[Name.identifier("level")] = EnumValue(
ClassId.topLevel(FqName("kotlin.DeprecationLevel")),
Name.identifier(level.name)
)
this[CirName.create("level")] = EnumValue(CirEntityId.create("kotlin/DeprecationLevel"), CirName.create(level.name))
},
annotationValueArguments = if (replaceWith != null) mapOf(Name.identifier("replaceWith") to replaceWith) else emptyMap()
annotationValueArguments = if (replaceWith != null) mapOf(CirName.create("replaceWith") to replaceWith) else emptyMap()
)
}
/*
* 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.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.name.ClassId
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame
import kotlin.test.assertTrue
class CirEntityIdTest {
@Test
fun createAndIntern() {
class TestRow(
val rawEntityId: String,
val packageSegments: Array<String>,
val rawRelativeNameSegments: List<String>
)
listOf(
TestRow(
rawEntityId = "",
packageSegments = emptyArray(),
rawRelativeNameSegments = emptyList()
),
TestRow(
rawEntityId = "/",
packageSegments = emptyArray(),
rawRelativeNameSegments = emptyList()
),
TestRow(
rawEntityId = "foo/",
packageSegments = arrayOf("foo"),
rawRelativeNameSegments = emptyList()
),
TestRow(
rawEntityId = "foo/bar/",
packageSegments = arrayOf("foo", "bar"),
rawRelativeNameSegments = emptyList()
),
TestRow(
rawEntityId = "foo/bar/baz/",
packageSegments = arrayOf("foo", "bar", "baz"),
rawRelativeNameSegments = emptyList()
),
TestRow(
rawEntityId = "My",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My")
),
TestRow(
rawEntityId = "My.Test",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My", "Test")
),
TestRow(
rawEntityId = "My.Test.Class",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My", "Test", "Class")
),
TestRow(
rawEntityId = "/My",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My")
),
TestRow(
rawEntityId = "/My.Test",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My", "Test")
),
TestRow(
rawEntityId = "/My.Test.Class",
packageSegments = emptyArray(),
rawRelativeNameSegments = listOf("My", "Test", "Class")
),
TestRow(
rawEntityId = "foo/My",
packageSegments = arrayOf("foo"),
rawRelativeNameSegments = listOf("My")
),
TestRow(
rawEntityId = "foo/My.Test",
packageSegments = arrayOf("foo"),
rawRelativeNameSegments = listOf("My", "Test")
),
TestRow(
rawEntityId = "foo/My.Test.Class",
packageSegments = arrayOf("foo"),
rawRelativeNameSegments = listOf("My", "Test", "Class")
),
TestRow(
rawEntityId = "foo/bar/My",
packageSegments = arrayOf("foo", "bar"),
rawRelativeNameSegments = listOf("My")
),
TestRow(
rawEntityId = "foo/bar/My.Test",
packageSegments = arrayOf("foo", "bar"),
rawRelativeNameSegments = listOf("My", "Test")
),
TestRow(
rawEntityId = "foo/bar/My.Test.Class",
packageSegments = arrayOf("foo", "bar"),
rawRelativeNameSegments = listOf("My", "Test", "Class")
),
TestRow(
rawEntityId = "foo/bar/baz/My",
packageSegments = arrayOf("foo", "bar", "baz"),
rawRelativeNameSegments = listOf("My")
),
TestRow(
rawEntityId = "foo/bar/baz/My.Test",
packageSegments = arrayOf("foo", "bar", "baz"),
rawRelativeNameSegments = listOf("My", "Test")
),
TestRow(
rawEntityId = "foo/bar/baz/My.Test.Class",
packageSegments = arrayOf("foo", "bar", "baz"),
rawRelativeNameSegments = listOf("My", "Test", "Class")
)
).forEach { testRow ->
with(testRow) {
// nullable, because ClassId may not have empty class name
val classifierId: ClassId? = if (rawRelativeNameSegments.isNotEmpty()) ClassId.fromString(rawEntityId) else null
val packageName = CirPackageName.create(packageSegments)
val relativeNameSegments = rawRelativeNameSegments.map(CirName::create).toTypedArray()
val entityIds = listOfNotNull(
CirEntityId.create(rawEntityId),
CirEntityId.create(rawEntityId),
classifierId?.let(CirEntityId::create),
classifierId?.let(CirEntityId::create),
CirEntityId.create(packageName, relativeNameSegments),
CirEntityId.create(packageName, relativeNameSegments)
)
val first = entityIds.first()
entityIds.forEach { entityId ->
assertSame(first, entityId)
assertSame(packageName, entityId.packageName)
assertTrue(relativeNameSegments.contentEquals(entityId.relativeNameSegments))
}
}
}
}
@Test
fun toStringConversion() {
listOf(
"" to "/",
"/" to "/",
"foo/" to "foo/",
"foo/bar/" to "foo/bar/",
"foo/bar/baz/" to "foo/bar/baz/",
"My" to "/My",
"My.Test" to "/My.Test",
"My.Test.Class" to "/My.Test.Class",
"foo/My" to "foo/My",
"foo/bar/My.Test" to "foo/bar/My.Test",
"foo/bar/baz/My.Test.Class" to "foo/bar/baz/My.Test.Class"
).forEach { (rawEntityId, asStringRepresentation) ->
assertEquals(asStringRepresentation, CirEntityId.create(rawEntityId).toString())
}
}
@Test
fun createNested() {
val nested1 = CirName.create("Nested1")
val nested2 = CirName.create("Nested2")
listOf(
"",
"/",
"foo/",
"foo/bar/",
"Outer",
"/Outer",
"foo/Outer",
"foo/bar/Outer",
"Outer.Nested",
"/Outer.Nested",
"foo/Outer.Nested",
"foo/bar/Outer.Nested"
).forEach { rawEntityId ->
val entityId = CirEntityId.create(rawEntityId)
val n1 = entityId.createNestedEntityId(nested1)
assertSame(entityId.packageName, n1.packageName)
assertEquals(entityId.relativeNameSegments.isNotEmpty(), n1.isNestedEntity)
assertEquals(entityId.relativeNameSegments.toList(), n1.relativeNameSegments.dropLast(1))
val n2 = n1.createNestedEntityId(nested2)
assertSame(entityId.packageName, n2.packageName)
assertTrue(n2.isNestedEntity)
assertEquals(entityId.relativeNameSegments.toList(), n2.relativeNameSegments.dropLast(2))
}
}
@Test
fun isNested() {
listOf(
"" to false,
"/" to false,
"foo/" to false,
"foo/bar/" to false,
"My" to false,
"My.Test" to true,
"My.Test.Class" to true,
"/My" to false,
"/My.Test" to true,
"/My.Test.Class" to true,
"foo/My" to false,
"foo/My.Test" to true,
"foo/My.Test.Class" to true,
"foo/bar/My" to false,
"foo/bar/My.Test" to true,
"foo/bar/My.Test.Class" to true,
).forEach { (rawEntityId, isNested) ->
assertEquals(isNested, CirEntityId.create(rawEntityId).isNestedEntity)
}
}
}
/*
* 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.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.name.Name
import org.junit.Test
import kotlin.test.assertSame
import kotlin.test.assertEquals
class CirNameTest {
@Test
fun createAndIntern() {
listOf("", "foo", "bar", "<stdlib>").forEach { rawName ->
val kotlinName = Name.guessByFirstCharacter(rawName)
val names = listOf(
CirName.create(rawName),
CirName.create(rawName),
CirName.create(kotlinName),
CirName.create(kotlinName)
)
val first = names.first()
names.forEach { name ->
assertSame(first, name)
}
}
}
@Test
fun toStringConversion() {
listOf("", "foo", "bar", "<stdlib>").forEach { rawName ->
val name = CirName.create(rawName)
assertEquals(rawName, name.name)
assertEquals(rawName, name.toString())
}
}
@Test
fun toStrippedStringConversion() {
listOf(
"" to "",
"foo" to "foo",
"bar" to "bar",
"<stdlib>" to "stdlib"
).forEach { (rawName, strippedName) ->
assertEquals(strippedName, CirName.create(rawName).toStrippedString())
}
}
}
/*
* 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.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirPackageName
import org.jetbrains.kotlin.name.FqName
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
class CirPackageNameTest {
@Test
fun createAndIntern() {
listOf(
"" to emptyArray(),
"foo" to arrayOf("foo"),
"foo.bar" to arrayOf("foo", "bar"),
"foo.bar.baz" to arrayOf("foo", "bar", "baz")
).forEach { (rawPackageFqName, segments) ->
val packageFqName = FqName(rawPackageFqName)
val packageNames = listOf(
CirPackageName.create(rawPackageFqName),
CirPackageName.create(rawPackageFqName),
CirPackageName.create(packageFqName),
CirPackageName.create(packageFqName),
CirPackageName.create(segments),
CirPackageName.create(segments)
)
val first = packageNames.first()
packageNames.forEach { packageName ->
assertTrue(segments.contentEquals(packageName.segments))
assertSame(first, packageName)
}
}
}
@Test
fun createRoot() {
val rootPackageNames = listOf(
CirPackageName.create(""),
CirPackageName.create(FqName("")),
CirPackageName.create(FqName.ROOT),
CirPackageName.create(emptyArray())
)
val first = rootPackageNames.first()
rootPackageNames.forEach { rootPackageName ->
assertSame(first, rootPackageName)
assertTrue(rootPackageName.segments.isEmpty())
assertTrue(rootPackageName.isRoot())
assertSame(CirPackageName.ROOT, rootPackageName)
}
}
@Test
fun toStringConversion() {
listOf(
"" to emptyArray(),
"foo" to arrayOf("foo"),
"foo.bar" to arrayOf("foo", "bar"),
"foo.bar.baz" to arrayOf("foo", "bar", "baz")
).forEach { (rawPackageFqName, segments) ->
assertEquals(rawPackageFqName, CirPackageName.create(segments).toString())
}
}
@Test
fun toMetadataStringConversion() {
listOf(
"" to "",
"foo" to "foo",
"foo.bar" to "foo/bar",
"foo.bar.baz" to "foo/bar/baz"
).forEach { (rawPackageFqName, metadataPackageFqName) ->
assertEquals(metadataPackageFqName, CirPackageName.create(rawPackageFqName).toMetadataString())
}
}
@Test
fun startsWith() {
val packageNames = listOf("", "foo", "foo.bar", "foo.bar.baz").map(CirPackageName::create)
for (i in packageNames.indices) {
val a = packageNames[i]
for (j in packageNames.indices) {
val b = packageNames[j]
assertEquals(i >= j, a.startsWith(b))
}
}
}
@Test
fun notStartsWith() {
listOf(
"aa" to "ab",
"aa.bb" to "aa.bc",
"aa.bb" to "aa.bb.cc"
).forEach { (a, b) ->
assertFalse(CirPackageName.create(a).startsWith(CirPackageName.create(b)))
}
}
}
......@@ -9,16 +9,16 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.TypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.LeafTarget
import org.jetbrains.kotlin.descriptors.commonizer.SharedTarget
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirClassFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeAliasFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.classifierId
import org.jetbrains.kotlin.descriptors.commonizer.utils.isUnderStandardKotlinPackages
import org.jetbrains.kotlin.descriptors.commonizer.utils.mockClassType
import org.jetbrains.kotlin.descriptors.commonizer.utils.mockTAType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.getAbbreviation
......@@ -37,7 +37,7 @@ class TypeCommonizerTest : AbstractCommonizerTest<CirType, CirType>() {
forwardDeclarations = CirForwardDeclarations.default(),
dependeeLibraries = mapOf(
FAKE_SHARED_TARGET to object : CirProvidedClassifiers {
override fun hasClassifier(classifierId: ClassId): Boolean = classifierId.packageFqName.isUnderStandardKotlinPackages
override fun hasClassifier(classifierId: CirEntityId): Boolean = classifierId.packageName.isUnderStandardKotlinPackages
}
)
)
......@@ -476,7 +476,7 @@ class TypeCommonizerTest : AbstractCommonizerTest<CirType, CirType>() {
val descriptor = (type.getAbbreviation() ?: type).constructor.declarationDescriptor
when (descriptor) {
is ClassDescriptor -> {
val classId = descriptor.classId ?: error("No class ID for ${descriptor::class.java}, $descriptor")
val classId = descriptor.classifierId
val node = classifiers.classNode(classId) {
buildClassNode(
storageManager = LockBasedStorageManager.NO_LOCKS,
......@@ -489,7 +489,7 @@ class TypeCommonizerTest : AbstractCommonizerTest<CirType, CirType>() {
node.targetDeclarations[index] = CirClassFactory.create(descriptor)
}
is TypeAliasDescriptor -> {
val typeAliasId = descriptor.classId ?: error("No class ID for ${descriptor::class.java}, $descriptor")
val typeAliasId = descriptor.classifierId
val node = classifiers.typeAliasNode(typeAliasId) {
buildTypeAliasNode(
storageManager = LockBasedStorageManager.NO_LOCKS,
......@@ -539,10 +539,10 @@ class TypeCommonizerTest : AbstractCommonizerTest<CirType, CirType>() {
private val FAKE_SHARED_TARGET = SharedTarget(setOf(LeafTarget("a"), LeafTarget("b")))
private fun CirKnownClassifiers.classNode(classId: ClassId, computation: () -> CirClassNode) =
private fun CirKnownClassifiers.classNode(classId: CirEntityId, computation: () -> CirClassNode) =
commonized.classNode(classId) ?: computation()
private fun CirKnownClassifiers.typeAliasNode(typeAliasId: ClassId, computation: () -> CirTypeAliasNode) =
private fun CirKnownClassifiers.typeAliasNode(typeAliasId: CirEntityId, computation: () -> CirTypeAliasNode) =
commonized.typeAliasNode(typeAliasId) ?: computation()
}
}
......@@ -5,12 +5,12 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
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
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeParameterFactory
import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS
import org.jetbrains.kotlin.descriptors.commonizer.utils.mockClassType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
import org.junit.Test
......@@ -90,7 +90,7 @@ class TypeParameterCommonizerTest : AbstractCommonizerTest<CirTypeParameter, Cir
upperBounds: List<String> = listOf("kotlin.Any")
) = CirTypeParameterFactory.create(
annotations = emptyList(),
name = Name.identifier(name),
name = CirName.create(name),
isReified = isReified,
variance = variance,
upperBounds = upperBounds.map { CirTypeFactory.create(mockClassType(it)) }
......
......@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
......@@ -14,7 +15,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.core.TypeCommonizerTest.Compa
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS
import org.jetbrains.kotlin.descriptors.commonizer.utils.mockClassType
import org.jetbrains.kotlin.name.Name
import org.junit.Test
class ValueParameterCommonizerTest : AbstractCommonizerTest<CirValueParameter, CirValueParameter>() {
......@@ -158,7 +158,7 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest<CirValueParameter, C
val returnType = CirTypeFactory.create(mockClassType(returnTypeFqName))
return CirTestValueParameter(
name = Name.identifier(name),
name = CirName.create(name),
annotations = emptyList(),
returnType = returnType,
varargElementType = returnType.takeIf { hasVarargElementType }, // the vararg type itself does not matter here, only it's presence matters
......@@ -171,7 +171,7 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest<CirValueParameter, C
}
internal data class CirTestValueParameter(
override val name: Name,
override val name: CirName,
override val annotations: List<CirAnnotation>,
override val returnType: CirType,
override val varargElementType: CirType?,
......
......@@ -13,14 +13,14 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.commonizer.*
import org.jetbrains.kotlin.descriptors.commonizer.ResultsConsumer.ModuleResult
import org.jetbrains.kotlin.descriptors.commonizer.ModulesProvider.ModuleInfo
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirClassFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.impl.AbstractTypeAliasDescriptor
import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorImpl
import org.jetbrains.kotlin.library.SerializedMetadata
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.parentOrNull
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
......@@ -126,7 +126,7 @@ internal val MOCK_CLASSIFIERS = CirKnownClassifiers(
LockBasedStorageManager.NO_LOCKS.createNullableLazyValue {
CirClassFactory.create(
annotations = emptyList(),
name = Name.identifier("kotlin.Any"),
name = CirName.create("Any"),
typeParameters = emptyList(),
visibility = DescriptorVisibilities.PUBLIC,
modality = Modality.OPEN,
......@@ -139,17 +139,17 @@ internal val MOCK_CLASSIFIERS = CirKnownClassifiers(
isExternal = false
)
},
ClassId.fromString("kotlin/Any")
CirEntityId.create("kotlin/Any")
)
override fun classNode(classId: ClassId) = MOCK_CLASS_NODE
override fun typeAliasNode(typeAliasId: ClassId) = error("This method should not be called")
override fun addClassNode(classId: ClassId, node: CirClassNode) = error("This method should not be called")
override fun addTypeAliasNode(typeAliasId: ClassId, node: CirTypeAliasNode) = error("This method should not be called")
override fun classNode(classId: CirEntityId) = MOCK_CLASS_NODE
override fun typeAliasNode(typeAliasId: CirEntityId) = error("This method should not be called")
override fun addClassNode(classId: CirEntityId, node: CirClassNode) = error("This method should not be called")
override fun addTypeAliasNode(typeAliasId: CirEntityId, node: CirTypeAliasNode) = error("This method should not be called")
},
forwardDeclarations = object : CirForwardDeclarations {
override fun isExportedForwardDeclaration(classId: ClassId) = false
override fun addExportedForwardDeclaration(classId: ClassId) = error("This method should not be called")
override fun isExportedForwardDeclaration(classId: CirEntityId) = false
override fun addExportedForwardDeclaration(classId: CirEntityId) = error("This method should not be called")
},
dependeeLibraries = emptyMap()
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册