[IR] Remove code duplication in IrTypeCheckerContext

This commit is contained in:
Roman Artemev
2019-07-10 10:45:39 +03:00
committed by romanart
parent c08b88281c
commit f9336555da
2 changed files with 6 additions and 21 deletions
@@ -19,28 +19,12 @@ class IrTypeCheckerContext(override val irBuiltIns: IrBuiltIns) : IrTypeSystemCo
override fun substitutionSupertypePolicy(type: SimpleTypeMarker): SupertypesPolicy.DoCustomTransform {
require(type is IrSimpleType)
val parameters = extractTypeParameters((type.classifier as IrClassSymbol).owner).map { it.symbol }
val substitution = parameters.zip(type.arguments).toMap()
val typeSubstitutor = IrTypeSubstitutor(parameters, type.arguments, irBuiltIns)
return object : SupertypesPolicy.DoCustomTransform() {
override fun transformType(context: AbstractTypeCheckerContext, type: KotlinTypeMarker): SimpleTypeMarker {
require(type is IrSimpleType)
return substituteArguments(type)
}
private fun substituteArguments(type: IrSimpleType): IrSimpleType {
val realArguments = type.arguments.map {
substitute(it)
}.toList()
return IrSimpleTypeImpl(type.classifier, type.hasQuestionMark, realArguments, type.annotations)
}
private fun substitute(type: IrTypeArgument): IrTypeArgument {
if (type is IrStarProjection) return type
val actualType = (type as IrTypeProjection).type as IrSimpleType
substitution[actualType.classifier]?.let { return it }
return makeTypeProjection(substituteArguments(actualType), type.variance)
require(type is IrType)
return typeSubstitutor.substitute(type) as IrSimpleType
}
}
}
@@ -11,13 +11,14 @@ import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.types.impl.toBuilder
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.types.model.TypeSubstitutorMarker
class IrTypeSubstitutor(
typeParameters: List<IrTypeParameterSymbol>,
typeArguments: List<IrTypeArgument>,
private val irBuiltIns: IrBuiltIns
) {
): TypeSubstitutorMarker {
init {
assert(typeParameters.size == typeArguments.size) {
"Unexpected number of type arguments: ${typeArguments.size}\n" +