[IR] Support IrType in type checker
This commit is contained in:
committed by
Dmitry Petrov
parent
c77f18fbe6
commit
d4e561baf6
@@ -20,6 +20,8 @@ import org.jetbrains.kotlin.descriptors.*
|
|||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
|
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
|
||||||
import org.jetbrains.kotlin.ir.util.IrSymbolVisitor
|
import org.jetbrains.kotlin.ir.util.IrSymbolVisitor
|
||||||
|
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||||
|
import org.jetbrains.kotlin.types.model.TypeParameterMarker
|
||||||
|
|
||||||
interface IrSymbol {
|
interface IrSymbol {
|
||||||
val owner: IrSymbolOwner
|
val owner: IrSymbolOwner
|
||||||
@@ -79,7 +81,7 @@ interface IrFieldSymbol :
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface IrClassifierSymbol :
|
interface IrClassifierSymbol :
|
||||||
IrSymbol {
|
IrSymbol, TypeConstructorMarker {
|
||||||
|
|
||||||
override val descriptor: ClassifierDescriptor
|
override val descriptor: ClassifierDescriptor
|
||||||
|
|
||||||
@@ -95,7 +97,7 @@ interface IrClassSymbol :
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface IrTypeParameterSymbol :
|
interface IrTypeParameterSymbol :
|
||||||
IrClassifierSymbol, IrBindableSymbol<TypeParameterDescriptor, IrTypeParameter> {
|
IrClassifierSymbol, IrBindableSymbol<TypeParameterDescriptor, IrTypeParameter>, TypeParameterMarker {
|
||||||
|
|
||||||
override fun <D, R> accept(visitor: IrSymbolVisitor<R, D>, data: D): R =
|
override fun <D, R> accept(visitor: IrSymbolVisitor<R, D>, data: D): R =
|
||||||
visitor.visitTypeParameterSymbol(this, data)
|
visitor.visitTypeParameterSymbol(this, data)
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ package org.jetbrains.kotlin.ir.types
|
|||||||
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
|
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
|
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
|
||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
|
import org.jetbrains.kotlin.types.model.*
|
||||||
|
|
||||||
interface IrType : IrAnnotationContainer {
|
interface IrType : KotlinTypeMarker, IrAnnotationContainer {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return true if this type is equal to [other] symbolically. Note that this is NOT EQUIVALENT to the full type checking algorithm
|
* @return true if this type is equal to [other] symbolically. Note that this is NOT EQUIVALENT to the full type checking algorithm
|
||||||
@@ -26,15 +27,15 @@ interface IrType : IrAnnotationContainer {
|
|||||||
|
|
||||||
interface IrErrorType : IrType
|
interface IrErrorType : IrType
|
||||||
|
|
||||||
interface IrDynamicType : IrType
|
interface IrDynamicType : IrType, DynamicTypeMarker
|
||||||
|
|
||||||
interface IrSimpleType : IrType {
|
interface IrSimpleType : IrType, SimpleTypeMarker, TypeArgumentListMarker {
|
||||||
val classifier: IrClassifierSymbol
|
val classifier: IrClassifierSymbol
|
||||||
val hasQuestionMark: Boolean
|
val hasQuestionMark: Boolean
|
||||||
val arguments: List<IrTypeArgument>
|
val arguments: List<IrTypeArgument>
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IrTypeArgument {
|
interface IrTypeArgument: TypeArgumentMarker {
|
||||||
override fun equals(other: Any?): Boolean
|
override fun equals(other: Any?): Boolean
|
||||||
|
|
||||||
override fun hashCode(): Int
|
override fun hashCode(): Int
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* 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.ir.types
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||||
|
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
|
||||||
|
import org.jetbrains.kotlin.types.AbstractTypeCheckerContext
|
||||||
|
import org.jetbrains.kotlin.types.Variance
|
||||||
|
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||||
|
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
|
||||||
|
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
|
||||||
|
|
||||||
|
class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemContext, AbstractTypeCheckerContext() {
|
||||||
|
|
||||||
|
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
|
||||||
|
return object : SupertypesPolicy.DoCustomTransform() {
|
||||||
|
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): SimpleTypeMarker {
|
||||||
|
return makeTypeProjection(type as IrType, Variance.INVARIANT) as SimpleTypeMarker
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areEqualTypeConstructors(a: TypeConstructorMarker, b: TypeConstructorMarker) = a === b
|
||||||
|
|
||||||
|
override fun intersectTypes(types: List<KotlinTypeMarker>): KotlinTypeMarker {
|
||||||
|
TODO("not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
override val isErrorTypeEqualsToAnything = false
|
||||||
|
override val KotlinTypeMarker.isAllowedTypeVariable: Boolean
|
||||||
|
get() = false
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
/*
|
||||||
|
* 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.ir.types
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.ClassKind
|
||||||
|
import org.jetbrains.kotlin.descriptors.Modality
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
|
||||||
|
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
|
||||||
|
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
|
||||||
|
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
|
||||||
|
import org.jetbrains.kotlin.types.checker.convertVariance
|
||||||
|
import org.jetbrains.kotlin.types.model.*
|
||||||
|
|
||||||
|
interface IrTypeSystemContext : TypeSystemContext {
|
||||||
|
|
||||||
|
val irBuiltIns: IrBuiltIns
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.asSimpleType() = this as? SimpleTypeMarker
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.asFlexibleType() = this as? IrDynamicType
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.isError() = this is IrErrorType
|
||||||
|
|
||||||
|
override fun FlexibleTypeMarker.asDynamicType() = this as? IrDynamicType
|
||||||
|
|
||||||
|
override fun FlexibleTypeMarker.asRawType(): RawTypeMarker? = null
|
||||||
|
|
||||||
|
override fun FlexibleTypeMarker.upperBound(): SimpleTypeMarker {
|
||||||
|
require(this is IrDynamicType)
|
||||||
|
return irBuiltIns.anyNType as IrSimpleType
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun FlexibleTypeMarker.lowerBound(): SimpleTypeMarker {
|
||||||
|
require(this is IrDynamicType)
|
||||||
|
return irBuiltIns.nothingType as IrSimpleType
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.asCapturedType(): CapturedTypeMarker? = null
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.asDefinitelyNotNullType(): DefinitelyNotNullTypeMarker? = null
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.isMarkedNullable(): Boolean = (this as IrSimpleType).hasQuestionMark
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.withNullability(nullable: Boolean): SimpleTypeMarker {
|
||||||
|
val simpleType = this as IrSimpleType
|
||||||
|
return if (simpleType.hasQuestionMark == nullable) simpleType
|
||||||
|
else simpleType.run { IrSimpleTypeImpl(classifier, nullable, arguments, annotations) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.typeConstructor() = (this as IrSimpleType).classifier
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.argumentsCount(): Int = (this as IrSimpleType).arguments.size
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.getArgument(index: Int): TypeArgumentMarker {
|
||||||
|
val simpleType = this as IrSimpleType
|
||||||
|
return simpleType.arguments[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.asTypeArgument() = this as IrTypeArgument
|
||||||
|
|
||||||
|
override fun CapturedTypeMarker.lowerType(): KotlinTypeMarker? = error("Captured Type is not valid for IrTypes")
|
||||||
|
|
||||||
|
override fun TypeArgumentMarker.isStarProjection() = this is IrStarProjection
|
||||||
|
|
||||||
|
override fun TypeArgumentMarker.getVariance() = (this as IrTypeProjection).variance.convertVariance()
|
||||||
|
|
||||||
|
override fun TypeArgumentMarker.getType() = (this as IrTypeProjection).type
|
||||||
|
|
||||||
|
private fun getTypeParameters(typeConstructor: TypeConstructorMarker): List<IrTypeParameter> {
|
||||||
|
val ownTypeParameterContainer = when(typeConstructor) {
|
||||||
|
is IrTypeParameterSymbol -> (typeConstructor.owner.parent as IrTypeParametersContainer)
|
||||||
|
is IrClassSymbol -> typeConstructor.owner
|
||||||
|
else -> error("unsupported type constructor")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ownTypeParameterContainer.typeParameters
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.parametersCount() = getTypeParameters(this).size
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.getParameter(index: Int) = getTypeParameters(this)[index].symbol
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.supertypes(): Collection<KotlinTypeMarker> {
|
||||||
|
return when (this) {
|
||||||
|
is IrClassSymbol -> owner.superTypes
|
||||||
|
is IrTypeParameterSymbol -> owner.superTypes
|
||||||
|
else -> error("unsupported type constructor")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isIntersection() = false
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isClassTypeConstructor() = this is IrClassSymbol
|
||||||
|
|
||||||
|
override fun TypeParameterMarker.getVariance() = (this as IrTypeParameterSymbol).owner.variance.convertVariance()
|
||||||
|
|
||||||
|
private fun getSuperTypes(typeParameterMarker: TypeParameterMarker) = (typeParameterMarker as IrTypeParameterSymbol).owner.superTypes
|
||||||
|
|
||||||
|
override fun TypeParameterMarker.upperBoundCount() = getSuperTypes(this).size
|
||||||
|
|
||||||
|
override fun TypeParameterMarker.getUpperBound(index: Int) = getSuperTypes(this)[index] as KotlinTypeMarker
|
||||||
|
|
||||||
|
override fun TypeParameterMarker.getTypeConstructor() = this as IrTypeParameterSymbol
|
||||||
|
|
||||||
|
override fun isEqualTypeConstructors(c1: TypeConstructorMarker, c2: TypeConstructorMarker) = (c1 === c2)
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isDenotable() = false
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isCommonFinalClassConstructor(): Boolean {
|
||||||
|
val classSymbol = this as? IrClassSymbol ?: return false
|
||||||
|
return classSymbol.owner.run { modality == Modality.FINAL && kind != ClassKind.ENUM_CLASS && kind != ClassKind.ANNOTATION_CLASS }
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: is that correct?
|
||||||
|
override fun captureFromArguments(type: SimpleTypeMarker, status: CaptureStatus): SimpleTypeMarker? = type
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.asArgumentList() = this as IrSimpleType
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isAnyConstructor() = this === irBuiltIns.anyClass
|
||||||
|
|
||||||
|
override fun TypeConstructorMarker.isNothingConstructor() = this === irBuiltIns.nothingClass
|
||||||
|
|
||||||
|
override fun KotlinTypeMarker.isNotNullNothing(): Boolean {
|
||||||
|
val simpleType = this as? IrSimpleType ?: return false
|
||||||
|
return simpleType.classifier === irBuiltIns.nothingClass && !simpleType.hasQuestionMark
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun SimpleTypeMarker.isSingleClassifierType() = true
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user