FirClassSubstitutionScope: extract SubstitutedData & reorder functions

This commit is contained in:
Mikhail Glukhikh
2020-06-24 11:30:57 +03:00
parent bf009a4949
commit 34a2196295
@@ -119,14 +119,7 @@ class FirClassSubstitutionScope(
}
if (skipPrivateMembers && member.visibility == Visibilities.PRIVATE) return original
val (newTypeParameters, newSubstitutor) = createNewTypeParametersAndSubstitutor(member)
val receiverType = member.receiverTypeRef?.coneTypeUnsafe<ConeKotlinType>()
val newReceiverType = receiverType?.substitute(newSubstitutor)
val returnType = typeCalculator.tryCalculateReturnType(member).type
val newReturnType = returnType.substitute(newSubstitutor)
val (newTypeParameters, newReceiverType, newReturnType, newSubstitutor) = createSubstitutedData(member)
val newParameterTypes = member.valueParameters.map {
it.returnTypeRef.coneTypeUnsafe<ConeKotlinType>().substitute(newSubstitutor)
}
@@ -156,12 +149,8 @@ class FirClassSubstitutionScope(
private fun createFakeOverrideConstructor(original: FirConstructorSymbol): FirConstructorSymbol {
if (substitutor == ConeSubstitutor.Empty) return original
val constructor = original.fir
val (newTypeParameters, newSubstitutor) = createNewTypeParametersAndSubstitutor(constructor)
val returnType = constructor.returnTypeRef.coneTypeUnsafe<ConeKotlinType>()
val newReturnType = returnType.substitute(newSubstitutor)
val (newTypeParameters, _, newReturnType, newSubstitutor) = createSubstitutedData(constructor)
val newParameterTypes = constructor.valueParameters.map {
it.returnTypeRef.coneTypeUnsafe<ConeKotlinType>().substitute(newSubstitutor)
}
@@ -175,6 +164,50 @@ class FirClassSubstitutionScope(
).symbol
}
private fun createFakeOverrideProperty(original: FirPropertySymbol): FirPropertySymbol {
if (substitutor == ConeSubstitutor.Empty) return original
val member = original.fir
if (skipPrivateMembers && member.visibility == Visibilities.PRIVATE) return original
val (newTypeParameters, newReceiverType, newReturnType) = createSubstitutedData(member)
if (newReceiverType == null &&
newReturnType == null && newTypeParameters === member.typeParameters
) {
return original
}
@Suppress("UNCHECKED_CAST")
return createFakeOverrideProperty(
session,
member,
original,
newReceiverType,
newReturnType,
newTypeParameters as List<FirTypeParameter>,
derivedClassId
)
}
private data class SubstitutedData(
val typeParameters: List<FirTypeParameterRef>,
val receiverType: ConeKotlinType?,
val returnType: ConeKotlinType?,
val substitutor: ConeSubstitutor
)
private fun createSubstitutedData(member: FirCallableMemberDeclaration<*>): SubstitutedData {
val (newTypeParameters, substitutor) = createNewTypeParametersAndSubstitutor(
member as FirTypeParameterRefsOwner
)
val receiverType = member.receiverTypeRef?.coneTypeUnsafe<ConeKotlinType>()
val newReceiverType = receiverType?.substitute(substitutor)
val returnType = typeCalculator.tryCalculateReturnType(member).type
val newReturnType = returnType.substitute(substitutor)
return SubstitutedData(newTypeParameters, newReceiverType, newReturnType, substitutor)
}
// Returns a list of type parameters, and a substitutor that should be used for all other types
private fun createNewTypeParametersAndSubstitutor(
member: FirTypeParameterRefsOwner
@@ -227,41 +260,6 @@ class FirClassSubstitutionScope(
)
}
private fun createFakeOverrideProperty(original: FirPropertySymbol): FirPropertySymbol {
if (substitutor == ConeSubstitutor.Empty) return original
val member = original.fir
if (skipPrivateMembers && member.visibility == Visibilities.PRIVATE) return original
val (newTypeParameters, newSubstitutor) = createNewTypeParametersAndSubstitutor(member)
val receiverType = member.receiverTypeRef?.coneTypeUnsafe<ConeKotlinType>()
val newReceiverType = receiverType?.substitute(newSubstitutor)
val returnType = typeCalculator.tryCalculateReturnType(member).type
val newReturnType = returnType.substitute(newSubstitutor)
if (newReceiverType == null &&
newReturnType == null && newTypeParameters === member.typeParameters
) {
return original
}
/*
* Member properties can't capture type parameters, so
* it's safe to cast newTypeParameters to List<FirTypeParameter>
*/
@Suppress("UNCHECKED_CAST")
return createFakeOverrideProperty(
session,
member,
original,
newReceiverType,
newReturnType,
newTypeParameters as List<FirTypeParameter>,
derivedClassId
)
}
private fun createFakeOverrideField(original: FirFieldSymbol): FirFieldSymbol {
if (substitutor == ConeSubstitutor.Empty) return original
val member = original.fir