Compare commits

...

1 Commits

Author SHA1 Message Date
Dmitriy Dolovov
a24ef27d05 TEMP: Simulate commonization of NSInteger/NSUInteger 2020-01-10 17:24:02 +07:00
4 changed files with 50 additions and 15 deletions

View File

@@ -10,7 +10,10 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.*
import org.jetbrains.kotlin.name.Name
class TypeAliasCommonizer(cache: CirClassifiersCache) : AbstractStandardCommonizer<CirTypeAlias, CirClass>() {
class TypeAliasCommonizer(
cache: CirClassifiersCache,
private val checkUnderlyingType: Boolean = true
) : AbstractStandardCommonizer<CirTypeAlias, CirClass>() {
private lateinit var name: Name
private val underlyingType = TypeCommonizer.default(cache)
private val visibility = VisibilityCommonizer.lowering(allowPrivate = true)
@@ -34,6 +37,6 @@ class TypeAliasCommonizer(cache: CirClassifiersCache) : AbstractStandardCommoniz
next.typeParameters.isEmpty() // TAs with declared type parameters can't be commonized
&& next.underlyingType.arguments.isEmpty() // TAs with functional types or types with parameters at the right-hand side can't be commonized
&& next.underlyingType.kind == CirSimpleTypeKind.CLASS // right-hand side could have only class
&& underlyingType.commonizeWith(next.underlyingType)
&& (!checkUnderlyingType || underlyingType.commonizeWith(next.underlyingType))
&& visibility.commonizeWith(next)
}

View File

@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.descriptors.commonizer.core.*
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.ir.CirRootNode.ClassifiersCacheImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroup
import org.jetbrains.kotlin.descriptors.commonizer.utils.firstNonNull
import org.jetbrains.kotlin.descriptors.commonizer.utils.isObjCIntegerType
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
@@ -123,18 +124,22 @@ internal fun buildTypeAliasNode(
storageManager: StorageManager,
cacheRW: ClassifiersCacheImpl,
typeAliases: List<TypeAliasDescriptor?>
): CirTypeAliasNode = buildNode(
storageManager = storageManager,
descriptors = typeAliases,
targetDeclarationProducer = ::CirWrappedTypeAlias,
commonValueProducer = { commonize(it, TypeAliasCommonizer(cacheRW)) },
recursionMarker = CirClassRecursionMarker,
nodeProducer = ::CirTypeAliasNode
).also { node ->
typeAliases.firstNonNull().fqNameSafe.let { fqName ->
node.fqName = fqName
cacheRW.typeAliases.putSafe(fqName, node)
}
): CirTypeAliasNode {
val fqName = typeAliases.firstNonNull().fqNameSafe
val node = buildNode(
storageManager = storageManager,
descriptors = typeAliases,
targetDeclarationProducer = ::CirWrappedTypeAlias,
commonValueProducer = { commonize(it, TypeAliasCommonizer(cacheRW, checkUnderlyingType = !fqName.isObjCIntegerType())) },
recursionMarker = CirClassRecursionMarker,
nodeProducer = ::CirTypeAliasNode
)
node.fqName = fqName
cacheRW.typeAliases.putSafe(fqName, node)
return node
}
private fun <D : Any, T : CirDeclaration, R : CirDeclaration, N : CirNode<T, R>> buildNode(

View File

@@ -0,0 +1,26 @@
/*
* 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.FqName
import org.jetbrains.kotlin.types.AbbreviatedType
import org.jetbrains.kotlin.types.KotlinType
private const val OBJECTIVE_C_SIGNED_INTEGER = "NSInteger"
private const val OBJECTIVE_C_UNSIGNED_INTEGER = "NSUInteger"
internal fun FqName.isObjCIntegerType(): Boolean {
if (!isUnderDarwinPackage)
return false
val name = shortName().asString()
return name == OBJECTIVE_C_SIGNED_INTEGER || name == OBJECTIVE_C_UNSIGNED_INTEGER
}
internal tailrec fun KotlinType.narrowType(): KotlinType =
if (this is AbbreviatedType) {
if (abbreviation.fqName.isObjCIntegerType()) abbreviation else expandedType.narrowType()
} else this

View File

@@ -22,7 +22,8 @@ internal inline val KotlinType.fqName: FqName
internal val KotlinType.fqNameWithTypeParameters: String
get() = buildString { buildFqNameWithTypeParameters(this@fqNameWithTypeParameters, HashSet()) }
private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, exploredTypeParameters: MutableSet<KotlinType>) {
private fun StringBuilder.buildFqNameWithTypeParameters(rawType: KotlinType, exploredTypeParameters: MutableSet<KotlinType>) {
val type = rawType.narrowType()
append(type.fqName)
val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)