[FIR] Split TypeUtils from :resolve to :cones and :tree modules
This commit is contained in:
@@ -13,7 +13,8 @@ import org.jetbrains.kotlin.name.ClassId
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
import kotlin.contracts.contract
|
||||
|
||||
inline fun <reified T : ConeKotlinType> FirTypeRef.coneTypeUnsafe() = (this as FirResolvedTypeRef).type as T
|
||||
inline fun <reified T : ConeKotlinType> FirTypeRef.coneTypeUnsafe(): T = (this as FirResolvedTypeRef).type as T
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
inline fun <reified T : ConeKotlinType> FirTypeRef.coneTypeSafe(): T? {
|
||||
contract {
|
||||
@@ -21,6 +22,7 @@ inline fun <reified T : ConeKotlinType> FirTypeRef.coneTypeSafe(): T? {
|
||||
}
|
||||
return (this as? FirResolvedTypeRef)?.type as? T
|
||||
}
|
||||
|
||||
inline val FirTypeRef.coneType: ConeKotlinType get() = coneTypeUnsafe()
|
||||
|
||||
val FirTypeRef.isAny: Boolean get() = isBuiltinType(StandardClassIds.Any, false)
|
||||
@@ -59,9 +61,9 @@ val FirFunctionTypeRef.parametersCount: Int
|
||||
const val EXTENSION_FUNCTION_ANNOTATION = "kotlin/ExtensionFunctionType"
|
||||
|
||||
val FirAnnotationCall.isExtensionFunctionAnnotationCall: Boolean
|
||||
get() = (this as? FirAnnotationCall)?.let {
|
||||
(it.annotationTypeRef as? FirResolvedTypeRef)?.let {
|
||||
(it.type as? ConeClassLikeType)?.let {
|
||||
get() = (this as? FirAnnotationCall)?.let { annotationCall ->
|
||||
(annotationCall.annotationTypeRef as? FirResolvedTypeRef)?.let { typeRef ->
|
||||
(typeRef.type as? ConeClassLikeType)?.let {
|
||||
it.lookupTag.classId.asString() == EXTENSION_FUNCTION_ANNOTATION
|
||||
}
|
||||
}
|
||||
@@ -97,4 +99,38 @@ fun List<FirAnnotationCall>.computeTypeAttributes(): ConeAttributes {
|
||||
}
|
||||
}
|
||||
return ConeAttributes.create(attributes)
|
||||
}
|
||||
}
|
||||
|
||||
fun FirTypeProjection.toConeTypeProjection(): ConeTypeProjection =
|
||||
when (this) {
|
||||
is FirStarProjection -> ConeStarProjection
|
||||
is FirTypeProjectionWithVariance -> {
|
||||
val type = typeRef.coneType
|
||||
type.toTypeProjection(this.variance)
|
||||
}
|
||||
else -> error("!")
|
||||
}
|
||||
|
||||
fun makesSenseToBeDefinitelyNotNull(type: ConeKotlinType): Boolean =
|
||||
type.canHaveUndefinedNullability() // TODO: also check nullability
|
||||
|
||||
fun ConeKotlinType.canHaveUndefinedNullability(): Boolean {
|
||||
return when (this) {
|
||||
is ConeTypeVariableType,
|
||||
is ConeCapturedType
|
||||
-> true
|
||||
is ConeTypeParameterType -> type.isMarkedNullable || !hasNotNullUpperBound()
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun ConeTypeParameterType.hasNotNullUpperBound(): Boolean {
|
||||
return lookupTag.typeParameterSymbol.fir.bounds.any {
|
||||
val boundType = it.coneType
|
||||
if (boundType is ConeTypeParameterType) {
|
||||
boundType.hasNotNullUpperBound()
|
||||
} else {
|
||||
boundType.nullability == ConeNullability.NOT_NULL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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.fir.types
|
||||
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterLookupTag
|
||||
import org.jetbrains.kotlin.fir.symbols.impl.ConeClassLikeLookupTagImpl
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
|
||||
import org.jetbrains.kotlin.fir.types.impl.ConeTypeParameterTypeImpl
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
fun ConeClassifierLookupTag.constructType(
|
||||
typeArguments: Array<out ConeTypeProjection>,
|
||||
isNullable: Boolean
|
||||
): ConeLookupTagBasedType {
|
||||
return when (this) {
|
||||
is ConeTypeParameterLookupTag -> ConeTypeParameterTypeImpl(this, isNullable)
|
||||
is ConeClassLikeLookupTag -> this.constructClassType(typeArguments, isNullable)
|
||||
else -> error("! ${this::class}")
|
||||
}
|
||||
}
|
||||
|
||||
fun ConeClassLikeLookupTag.constructClassType(
|
||||
typeArguments: Array<out ConeTypeProjection>,
|
||||
isNullable: Boolean,
|
||||
): ConeClassLikeType {
|
||||
return ConeClassLikeTypeImpl(this, typeArguments, isNullable)
|
||||
}
|
||||
|
||||
fun ClassId.constructClassLikeType(
|
||||
typeArguments: Array<out ConeTypeProjection>,
|
||||
isNullable: Boolean,
|
||||
): ConeClassLikeType {
|
||||
return ConeClassLikeTypeImpl(ConeClassLikeLookupTagImpl(this), typeArguments, isNullable)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user