[Core API] Introduce KotlinTypeRefiner component

This commit is contained in:
Dmitriy Novozhilov
2019-07-22 19:44:03 +03:00
committed by Dmitry Savvinov
parent 5c17148146
commit 2fe3a23183
4 changed files with 249 additions and 3 deletions
@@ -26,8 +26,10 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.checker.Ref
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.utils.sure
import java.lang.IllegalArgumentException
class ModuleDescriptorImpl @JvmOverloads constructor(
moduleName: Name,
@@ -35,13 +37,18 @@ class ModuleDescriptorImpl @JvmOverloads constructor(
override val builtIns: KotlinBuiltIns,
// May be null in compiler context, should be not-null in IDE context
override val platform: TargetPlatform? = null,
private val capabilities: Map<ModuleDescriptor.Capability<*>, Any?> = emptyMap(),
capabilities: Map<ModuleDescriptor.Capability<*>, Any?> = emptyMap(),
override val stableName: Name? = null
) : DeclarationDescriptorImpl(Annotations.EMPTY, moduleName), ModuleDescriptor {
private val capabilities: Map<ModuleDescriptor.Capability<*>, Any?>
init {
if (!moduleName.isSpecial) {
throw IllegalArgumentException("Module name must be special: $moduleName")
}
this.capabilities = capabilities.toMutableMap()
@UseExperimental(TypeRefinement::class)
this.capabilities[REFINER_CAPABILITY] = Ref(null)
}
private var dependencies: ModuleDependencies? = null
@@ -17,9 +17,9 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils.getContainingClass
import org.jetbrains.kotlin.platform.TargetPlatform
import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
@@ -29,6 +29,9 @@ import org.jetbrains.kotlin.types.TypeProjection
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.checker.KotlinTypeRefiner
import org.jetbrains.kotlin.types.checker.REFINER_CAPABILITY
import org.jetbrains.kotlin.types.refinement.TypeRefinement
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny
import org.jetbrains.kotlin.types.typeUtil.makeNullable
import org.jetbrains.kotlin.utils.DFS
@@ -441,3 +444,6 @@ fun DeclarationDescriptor.isAnnotationConstructor(): Boolean =
fun DeclarationDescriptor.isPrimaryConstructorOfInlineClass(): Boolean =
this is ConstructorDescriptor && this.isPrimary && this.constructedClass.isInline
@TypeRefinement
fun ModuleDescriptor.getKotlinTypeRefiner(): KotlinTypeRefiner = getCapability(REFINER_CAPABILITY)?.value ?: KotlinTypeRefiner.Default
@@ -0,0 +1,82 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. 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.types.checker
import org.jetbrains.kotlin.container.DefaultImplementation
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.UnwrappedType
import org.jetbrains.kotlin.types.refinement.TypeRefinement
@DefaultImplementation(impl = KotlinTypeRefiner.Default::class)
abstract class KotlinTypeRefiner {
@TypeRefinement
abstract fun refineType(type: KotlinType): KotlinType
@TypeRefinement
abstract fun refineSupertypes(classDescriptor: ClassDescriptor): Collection<KotlinType>
@TypeRefinement
abstract fun refineDescriptor(descriptor: DeclarationDescriptor): ClassifierDescriptor?
@TypeRefinement
abstract fun findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor?
@TypeRefinement
abstract fun isRefinementNeededForModule(moduleDescriptor: ModuleDescriptor): Boolean
@TypeRefinement
abstract fun isRefinementNeededForTypeConstructor(typeConstructor: TypeConstructor): Boolean
@TypeRefinement
abstract fun <S : MemberScope> getOrPutScopeForClass(classDescriptor: ClassDescriptor, compute: () -> S): S
object Default : KotlinTypeRefiner() {
@TypeRefinement
override fun refineType(type: KotlinType): KotlinType = type
@TypeRefinement
override fun refineSupertypes(classDescriptor: ClassDescriptor): Collection<KotlinType> {
return classDescriptor.typeConstructor.supertypes
}
@TypeRefinement
override fun refineDescriptor(descriptor: DeclarationDescriptor): ClassDescriptor? {
return null
}
@TypeRefinement
override fun findClassAcrossModuleDependencies(classId: ClassId): ClassDescriptor? {
return null
}
@TypeRefinement
override fun isRefinementNeededForModule(moduleDescriptor: ModuleDescriptor): Boolean {
return false
}
@TypeRefinement
override fun isRefinementNeededForTypeConstructor(typeConstructor: TypeConstructor): Boolean {
return false
}
@TypeRefinement
override fun <S : MemberScope> getOrPutScopeForClass(classDescriptor: ClassDescriptor, compute: () -> S): S {
return compute()
}
}
}
@TypeRefinement
fun KotlinTypeRefiner.refineTypes(types: Iterable<KotlinType>): List<KotlinType> = types.map { refineType(it) }
class Ref<T : Any>(var value: T?)
@TypeRefinement
val REFINER_CAPABILITY = ModuleDescriptor.Capability<Ref<KotlinTypeRefiner>>("KotlinTypeRefiner")